结构体的定义和使用
1.结构体的定义和使用
struct Student
{
int score; //特性;分数
char name[128];
int xuehao; //特性;名字
//void(*pintroduce)(char *pname);// 行为;函数指针(自我介绍)
};
int main()
{
// 类型 变量名 初始值
int a = 10;
struct Student stu1 = {98,"lao K"};
printf("a=%d\n",a);
printf("结构体;score = %d\n",stu1.score);
printf("结构体;name = %s\n",stu1.name);
关键字 struct(翻译;结构体)
模板的作用(数据有点模糊)
提醒;新手容易忘记在结构体}后面带上分好;
2. 如何访问结构体;目标,内部的变量
举例程序;
struct Student
{
int score; //特性;分数
char name[128];
int xuehao; //特性;名字
//void(*pintroduce)(char *pname);// 行为;函数指针(自我介绍)
};
int main()
{
struct Student stu1 = {98,"lao K"};
printf("结构体;score = %d\n",stu1.score);
printf("结构体;name = %s\n",stu1.name);
return 0;
}
运行结果;
注意;
struct Student test;
test.score = 99;
//test.name = "lao san";
strcpy(test.name,"lao san");
printf("结构体;score = %d\n",test.score);
printf("结构体;name = %s\n",test.name);
下面这种格式可以在c++,java中可以用
test.score = 99;
//test.name = "lao san";