2

I am learning C and relatively new to it.

I am having trouble with Structs and I am trying to get a structure variable to hold the values firstName, lastName, tries, won, and percentage. The last three themselves have to be contained in another struct inside the first struct. My code is below, also if anyone could explain the difference between structure tags and variable types that would help a lot. I understand there may be a lot of errors in the code.

#include <stdio.h>
#include <string.h>

struct team{
    char firstName[40];                     
    char lastName[40];

    struct stats{
        int tries;

        int won;

        float percentage;
    } record;
}; 

int main(){
  //Assign variable name and test print to check that struct is working.
  struct team player;
  strcpy(player.firstName,"Michael");
  strcpy(player.lastName,"Jordan");

  struct stats player;
  player.tries = 16;
  player.won = 14;
  player.percentage = ((player.won/player.tries)*100);

  printf("First Name: \t %s \n", player.firstName);
  printf("Last Name: \t %s \n", player.lastName);
  printf("Tries: \t %d \n", player.tries);
  printf("Won: \t %d \n", player.won);
  printf("Percentage: \t %f \n", player.percentage);

  return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294

4 Answers4

3

When accessing struct inside struct, do it like this:

player.record.tries = 16;
player.record.won = 14;
player.record.percentage = (((float)player.record.won/player.record.tries)*100);

As for the struct tag, with the struct team type, team is the struct tag, while struct team together makes a type. You may use typedef to use struct type without struct keyword.

typedef struct team team;
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

You know how to access structure members (e.g. player.firstname), accessing nested structure members are just the same, with the "dot" member selection operator:

player.record.tries = ...;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Is Record the name or variable type? I want it to be the type. When I put the above in my code, and change every reference to player.record.tries etc I get an error saying that the struct team has no member 'won' or 'tries'. –  Feb 21 '14 at 15:25
  • @mungostrap It's a structure member variable inside the `team` structure, just like e.g. `firstname`. – Some programmer dude Feb 21 '14 at 15:26
0

The compiler shall issue an error because you defined the same name player having different types

struct team player;                             //Assign variable name and test print to check that 
// ...
struct stats player;

You should write

struct team player;                             //Assign variable name and test print to check that struct is working.
strcpy(player.firstName,"Michael");
strcpy(player.lastName,"Jordan");

player.record.tries = 16;
player.record.won = 14;
player.record.percentage = ((player.record.won / player.record.tries)*100);

Or you could write

int main(){
  //Assign variable name and test print to check that struct is working.
  struct team player;
  strcpy(player.firstName,"Michael");
  strcpy(player.lastName,"Jordan");

  struct stats s;
  s.tries = 16;
  s.won = 14;
  s.percentage = ((s.won/s.tries)*100);

  player.record = s;
  // ,,,
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You can also save typing of struct everywhere by typedefing it. See this question and accepted answer for more.

Community
  • 1
  • 1
kbshimmyo
  • 578
  • 4
  • 13