结构体指针
1.结构体指针就是指向结构体变量的指针,一个结构体变量的起始地址就是这个结构体变量的指针。指向结构体对象的指针变量既可指向结构体变量,也可指向结构体数组中的元素。
例如:
struct student
{
int num;
char name[10];
char sex;
float score;
}struct student stu1[10]={
1,'a','b',2,'c','d',3,'e','f'
};
struct student *p=&struct student stu1[0];
printf("%d",*p); (1)
printf("%c",p->name); (a)
其中p指向结构体变量中的name成员用p->name表示,->称为指向运算符。
2、结构体数据类型大小是4的倍数,例如
struct student
{
int num;
char s;
char sex;
float score;
}就是4+(1+1)+4 ,12个字节,为什么是12个,每一排4个字节,第一排排满了4个,第二排排了1个+1个,剩下两个排不下float的4个字节,自动排到下一排,4个字节,所以一共是12个字节。如果是
int num;
char s;
int a;
char b;
就是4+1+4+1,16个字节,第一排4个字节,第二排一个字节,剩下三个放不下下一个int的4个字节,自动到下一排,排int的4个字节,然后最后一排一个字节。所以一共是4*4=16个字节。
3、结构体的操作方式有增、删、修改、查找、排序。
结构体是唯一一个可以整体赋值的,例如:
struct student stu1[10];
stu[0]=stu[1];
typedef 规定一种数据类型,在struct里可以有可以没有,起到一个声明的作用。