Quick actions

cmd+k|ctrl+k

Navigation

Languages

结构体嵌套(分开定义)

Snippet info

Language

C

Visibility

public

Author

qwas

Created

2024-05-10T09:44:14.289763Z

Updated

2024-05-10T09:47:09.082355Z

#include <stdio.h>
struct scorestruct
{
  int math;
  int english;
} scores;

struct infostruct
{
  float height;
  float weight;
} info;

struct student
{
  char name[20];
  struct scorestruct scores; /* scorestruct 型的结构体变量 score,作为 students 结构的数据成员 */
  struct infostruct info;
};

// 结构体嵌套
int main()
{
  struct student wangyu = {"Wang wu", {80, 96}, {175, 80}};
  printf("wangwu's name: %s \n", wangyu.name);
  printf("wangwu's math scores: %d \n", wangyu.scores.math);
  printf("wangwu's english scores: %d \n", wangyu.scores.english);
  printf("wangwu's height: %.1f \n", wangyu.info.height);
  printf("wangwu's weight: %.1f \n", wangyu.info.weight);

  return 0;
}
INFO