user3334282
user3334282

Reputation:

Structure inside structure in C

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;
}

Upvotes: 2

Views: 771

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311146

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;
  // ,,,

Upvotes: 0

kbshimmyo
kbshimmyo

Reputation: 579

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

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122493

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;

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409482

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 = ...;

Upvotes: 1

Related Questions