#include <stdio.h>
#include <string.h>
//无名结构体:没有类型名
struct {
int id;
float score;
char name[32];
}s1 = {1, 91.38, "里斯"};
int main(int argc, const char *argv[])
{
printf("id=%d score=%.1f name=%s\n",s1.id, s1.score, s1.name);
s1.id = 0;
s1.score = 79.23;
strcpy(s1.name,"张三");
printf("id=%d score=%.1f name=%s\n",s1.id, s1.score, s1.name);
return 0;
}
#include <stdio.h>
#include <string.h>
//结构体嵌套
struct stu{
int id;
char name[32];
struct { //结构体嵌套,内部一般使用无名结构体
int month;
int year;
}d;
}s;
int main(int argc, const char *argv[])
{
s.id = 3;
strcpy(s.name, "王五");
s.d.month = 10;
s.d.year = 2016;
printf("id = %d\n",s.id);
printf("name = %s\n",s.name);
printf("mont = %d\n",s.d.month);
printf("year = %d\n",s.d.year);
return 0;
}
#include <stdio.h>
#include <string.h>
struct stu{
int id;
char name[32];
struct day{
int month;
int year;
}d;
}s = {4, "赵", {5,2014}};
int main(int argc, const char *argv[])
{
printf("id = %d\n",s.id);
printf("name = %s\n",s.name);
printf("mont = %d\n",s.d.month);
printf("year = %d\n",s.d.year);
s.id = 3;
strcpy(s.name, "王五");
s.d.month = 10;
s.d.year = 2016;
puts("***************");
printf("id = %d\n",s.id);
printf("name = %s\n",s.name);
printf("mont = %d\n",s.d.month);
printf("year = %d\n",s.d.year);
struct day d1 = {12,2015}; //struct day 可以单独使用
printf("%d\n",d1.month);
return 0;
}