定义结构体变量有三种方式:
(1) 用结构体类型定义变量
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
struct Teacher {
int age;
char name[64];
};
int main()
{
struct Teacher t1;
t1.age = 12;
strcpy(t1.name,"hello");
printf("%d \n", t1.age);
printf("%s \n", t1.name);
}
用typedef定义一个结构体别名,注意与上一个定义结构体的方式的语法的区别:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Teacher {
int age;
char name[64];
} Teach;
int main()
{
Teach t1;
t1.age = 12;
strcpy(t1.name,"hello");
printf("%d \n", t1.age);
printf("%s \n", t1.name);
}
(2) 定义类型的同时,定义变量
struct _Student
{
ch