1.結構體 定義和賦值
struct students
{
//成員列表
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
}stu = { "尼古拉斯",500,"13888888888",100.0f,200,300,'M' };
int main02()
{
printf("姓名:%s\n", stu.name);
printf("年齡:%d\n", stu.age);
printf("電話: %s\n", stu.tel);
printf("成績: %.1f %.1f %.1f\n", stu.scores[0], stu.scores[1], stu.scores[2]);
printf("性別: %s\n", stu.sex == 'M' ? "男" : "女");
system("pause");
return EXIT_SUCCESS;
}
2.第二種按照結構體順序 賦值
struct students
{ //成員列表
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex; }
int main()
{
struct students stu = { "尼古拉斯",500,"13888888888",100.0f,200,300,'M' }; }
3.第三種賦值
struct students
{
//成員列表
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
}
int main()
{
strcpy(stu.name, "謝廣坤");
stu.age = 50;
strcpy(stu.tel, "13777777778");
stu.scores[0] = 90;
stu.scores[1] = 80;
stu.scores[2] = 70;
stu.sex = 'F';
}
二.結構體數組
struct stu
{
//成員列表
char name[21];
unsigned int age;
char tel[16];
float scores[3];//scores[0]
char sex;
};
int main04()
{
//結構體數組
struct stu s[2];
for (int i = 0; i < 2; i++)
{
printf("請您輸入 姓名 年齡 電話 成績 性別:\n");
scanf("%s%d%s%f%f%f,%c", s[i].name, &s[i].age, s[i].tel, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2], &s[i].sex);
}
for (int i = 0; i < 2; i++)
{
printf("姓名:%s\n", s[i].name);
printf("年齡:%d\n", s[i].age);
printf("電話: %s\n", s[i].tel);
printf("成績: %.1f %.1f %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
printf("性別: %s\n", s[i].sex == 'M' ? "男" : "女");
}
system("pause");
return EXIT_SUCCESS;
}
三.堆空間開闢結構體
1.使用malloc創建一個 結構體類型的指針 p ,訪問結構體內部數據時,使用 p->name ,而內部name又是一個char類型指針
2.再爲name開闢堆內存,再使用strcpy賦值 3.釋放時,先釋放小的,再釋放打大的
struct tec
{
char *name; //4
int age; //4
}t;
int main()
{
struct tec * p = (struct tec *)malloc(sizeof(t));
p->name = (char *)malloc(sizeof(char) * 21);
strcpy(p->name, "牛玲");
p->age = 18;
printf("%s %d\n", p->name, p->age);
if (p->name != NULL)
{
free(p->name);
p->name = NULL;
}
if (p)
{
free(p);
p = NULL;
}
printf("%d\n", sizeof(struct tec *));
system("pause");
return EXIT_SUCCESS;
}
四.結構體嵌套
struct stra
{
int a; //4 4
float b;//4 8
char c;//1 12 9
char arr[7]; //16
double h;//24
}abc;
struct strb
{
struct stra abc;//12 16
short f; //2
char * e; //4
short g;
//double d; //8
};
二.聯合體(共用體)
1.求共用體 的sizeof 是看最大的數據類型 2.共用體最後被賦值的數據 c纔是準確的 其他的可能被覆蓋
union vars
{
double a;
float b;
int c;
}var;
printf("%d\n", sizeof(var)); 8
var.a = 100;
var.b = 3.14;
var.c = 66;
printf("%f\n", var.a); 100.000000
printf("%f\n", var.b); 0.000000
printf("%d\n", var.c); 66
三.枚舉
1.默認枚舉中的值從0 開始自增 2.如果給red手動賦值 10 那麼後面還是依次遞增1
enum colors
{
red,bule,yellow,black,white,green
}clo;
int main14()
{
int val;
scanf("%d", &val);
switch (val)
{
case red:
break;
case bule:
break;
case yellow:
break;
case black:
break;
case white:
break;
case green:
break;
default:
break;
}
system("pause");
return EXIT_SUCCESS;
}
四.typedef
簡化命名, 起別名
typedef unsigned long long ull;
struct studentsInfoList
{
char name[20];
char sex;
};
typedef struct studentsInfoList sinfo;
int main115()
{
sinfo s1;
ull a = 10;
printf("%d\n", a); 10 }