结构体定于与使用
struct:结构体函数的定义
struct Student {
char name[10];
int age;
char sex;
};
赋值1:
在初始化 Student
的时候,默认数值是系统值。字符串需要通过 strcpy
赋值
int main() {
struct Student student;
printf("name: %s, age:%d, sex:%c \n",student.name,student.age,student.sex);
strcpy(student.name,"Lees");
student.age = 12;
student.sex ='N';
printf("name: %s, age:%d, sex:%c \n",student.name,student.age,student.sex);
return NULL;
}
//输出
name:, age:0, sex:?
name: Lees, age:12, sex:N
赋值2:
声明函数体的时候就定义了初始化数值
struct Student {
char name[10];
int age;
char sex;
} lees = {"less", 18, 'M'};
int main() {
printf("name: %s, age:%d, sex:%c \n",lees.name,lees.age,lees.sex);
return NULL;
}
//输出
name: less, age:18, sex:M
结构体指针(栈)
struct Cat {
char name[10];
int age;
};
int main() {
struct Cat cat={"小花猫",2};
//VS的写法( 不需要 struct):Cat *catp =&cat;
struct Cat * catp = &cat;
//结构体指针, 一级指正
catp->age =3;
strcpy(catp->name,"小花猫2");
printf("naem:%s,age:%d \n",catp->name,catp->age);
return NULL;
}
//输出
naem:小花猫2,age:3
结构体指针(堆)
用关键函数 malloc()
定义开辟的空间,最后要手动释放free()
struct Cat2 {
char name[10];
int age;
};
int main() {
//开辟堆
struct Cat2 *cat = malloc(sizeof(struct Cat2));
cat->age = 10;
strcpy(cat->name, "大花猫");
printf("naem:%s,age:%d \n", cat->name, cat->age);
//释放销毁 堆
free(cat);
cat = NULL;
return NULL;
}
//输出
naem:大花猫,age:10
数组
现在定义了一个Cat的数组长度为10,分别静态栈与动态堆的方式赋值
struct Cat3 {
char name[10];
int age;
};
静态栈
int main() {
struct Cat3 cat[10] = {
{"小黄", 1},
{"小白", 2},
{"小黑", 3},
{"小花猫", 4},
{"大花猫", 5},
{},
{},
{},
{},
{},
};
struct Cat3 cat9 = {"大猫猫", 9};
//数组方式赋值
//cat[9] = cat9;
//指针方式辅助
*(cat + 9) = cat9;
int length = sizeof(cat) / sizeof(struct Cat3);
for (int i = 0; i < length; ++i) {
printf("当期的位置:%d \n",i);
printf("指针来取值: naem:%s,age:%d \n", (*(cat + i)).name, (*(cat + i)).age);
printf("数组来取值: naem:%s,age:%d \n", cat[i].name, cat[i].age);
printf("\n");
}
return NULL;
}
//输出
当期的位置:0
指针来取值: naem:小黄,age:1
数组来取值: naem:小黄,age:1
当期的位置:1
指针来取值: naem:小白,age:2
数组来取值: naem:小白,age:2
当期的位置:2
指针来取值: naem:小黑,age:3
数组来取值: naem:小黑,age:3
当期的位置:3
指针来取值: naem:小花猫,age:4
数组来取值: naem:小花猫,age:4
当期的位置:4
指针来取值: naem:大花猫,age:5
数组来取值: naem:大花猫,age:5
当期的位置:5
指针来取值: naem:,age:0
数组来取值: naem:,age:0
当期的位置:6
指针来取值: naem:,age:0
数组来取值: naem:,age:0
当期的位置:7
指针来取值: naem:,age:0
数组来取值: naem:,age:0
当期的位置:8
指针来取值: naem:,age:0
数组来取值: naem:,age:0
当期的位置:9
指针来取值: naem:大猫猫,age:9
数组来取值: naem:大猫猫,age:9
动态堆
int main() {
struct Cat3 *catx = malloc(10);
strcpy((catx + 9)->name, "大花猫");
(catx + 9)->age = 12;
printf("指正取值: naem:%s , age:%d \n", (catx + 9)->name, (catx + 9)->age);
for (int i = 0; i < 10; ++i) {
printf("当期的位置:%d \n", i);
printf("动态堆 指正取值: naem:%s,age:%d \n", catx[i].name, catx[i].age);
}
feof(catx);
catx = NULL;
}
//输出
指正取值: naem:大花猫 , age:12
// 遍历-》堆
当期的位置:0
动态堆 指正取值: naem:繿?age:0
当期的位置:1
动态堆 指正取值: naem:ive=C:,age:28678
当期的位置:2
动态堆 指正取值: naem:衠?age:0
当期的位置:3
动态堆 指正取值: naem:\OneDrive,age:1413563472
当期的位置:4
动态堆 指正取值: naem:A=C:\Users\Roaming,age:1281846885
当期的位置:5
动态堆 指正取值: naem:ee\AppData\Roaming,age:1768776047
当期的位置:6
动态堆 指正取值: naem:ng,age:1634887535
当期的位置:7
动态堆 指正取值: naem:m Files\to大花猫,age:1684947292
当期的位置:8
动态堆 指正取值: naem:roidSDK\build-to大花猫,age:1869884772
当期的位置:9
动态堆 指正取值: naem:大花猫,age:12