1. 结构就是多个变量的集合:
#include <stdio.h>
int main(void)
{
struct Rec {
int x;
int y;
};
struct Rec r1;
r1.x = 111;
r1.y = 222;
printf("%d, %d", r1.x, r1.y);
getchar();
return 0;
}
2. 定义时同时声明变量:
#include <stdio.h>
int main(void)
{
struct Rec {
int x,y;
} r1,r2;
r1.x = 111;
r1.y = 222;
r2.x = 333;
r2.y = 444;
printf("%d, %d\n", r1.x, r1.y);
printf("%d, %d\n", r2.x, r2.y);
getchar();
return 0;
}
3. 定义时同时声明变量并赋值:
#include <stdio.h>
int main(void)
{
struct Rec {
int x,y;
} r1 = {777,888};
printf("%d, %d\n", r1.x, r1.y);
getchar();
return 0;
}
#include <stdio.h>
int main(void)
{
struct Rec {
char name[12];
short age;
} r1 = {"ZhangSan", 12};
printf("%s, %u", r1.name, r1.age);
getchar();
return 0;
}