c语言结构体:
//结构体能以一整组数据赋值给另一组
//结构体所占数据大小必定是4的倍数
//每行所占字节4个,不够自动换行
#include <stdio.h>
struct stu{
int x;
char a;
char b;
};
//4 * 3 = 12
struct ver{
struct stu s;
char n;
float c;
}yy[10] = {1,'a','b','c',3.3};
struct stu ss[10];
int main(){
printf("%d\n",sizeof(stu));
printf("%d\n",yy[0].s.x);
//typedef规定数据类型,常与结构体连用
//正常定义struct前面有typedef,省略不影响使用
//typedef相当于声明作用
//typedef只能定义数据类型,不能定义结构体数组
结构体元素的几种表达形式:
#include <stdio.h>
struct stu{
int i;
char a;
char b;
}student[5] = {
1,'a','v',2,'c','p',3,'e','t'
};
int main(){
struct stu *p = &student[0];
printf("%d\n",*p);
printf("%c\n",p->a);
printf("%c\n",student[0].b);
printf("%d\n",*(p+1));
printf("%c\n",(*(p+1)).a);
printf("%c\n",(p+1)->b);
printf("%d\n",*(p+2));
}
网友评论