简体   繁体   中英

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

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;

您知道如何访问结构成员(例如player.firstname ),使用“点”成员选择运算符访问嵌套的结构成员是相同的:

player.record.tries = ...;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM