c 结构体 简单的了解

1.声明一个学生类的 结构体

struct Student{

int age;

char name[20];//长度为20的字符串

int weiht;//像正常一样的申请变量,这个变量属于结构体的一部分

};//这分号 必须有,不然报错

结构体和类差不多,必须有对象才能操作,如:stcuct Stdent std;

赋值:

std.age = 10;

std.weiht=100;

字符串没法整体赋值,所以使用函数strcpy,

strcy(std.name,"小明");

输出:

printf("我叫%s,我年龄:%d,体重:%d”,std.name,std.age,atd.weiht);

输出为:我叫小明,我年龄10,体重100.

赋值也可以在结构体声明时候初始化:

stcuct Stdent std={10,“小明”,100};

结构体数组:

struct Student s[100];

只要把这里的s[a]代替上边的的std即可。

a可以是0 - 100 以内的任意数。

原文地址:https://www.cnblogs.com/fgyqbs/p/3848235.html