//结构体分配内存是四个字节一行 struct Teacher{ char name[62]; //64 int age;//4 char c1; char c2; }; 大小为72 |
struct Teacher{ char c1; char c2; char name[62]; //64 int age;//4 }; 大小为68 |
变量做函数参数具有副本机制(将实参copy给形参),所以并不能真正改变实参值 void copyObj01(struct Teacher from, struct Teacher to) { //只是将实参两个值分别赋给了from和to,然后操作from和to }
void copyObj02(struct Teacher *from, struct Teacher *to) { //直接操作实参地址,通过地址间接修改实参值 } |
#include "stdio.h" #include "stdlib.h" #include "string.h"
//两个结构体变量之间的copy行为 struct AdvTeacher { char name[64];//64 char *a_name; //4 int age ; //4 };
int printtArray(struct AdvTeacher *pArray, int num) { int i = 0; for (i=0; i<num; i++) { printf("\n %d ", pArray[i].age); printf("%s ", pArray[i].name); printf("%s ", pArray[i].a_name); } return 0; }
int sorttArray(struct AdvTeacher *pArray, int num ) { int i = 0, j = 0; struct AdvTeacher tmp; for (i=0; i<4; i++) { for (j=i+1; j<num; j++) { if (pArray[i].age < pArray[j].age) { tmp = pArray[i]; pArray[i] = pArray[j]; pArray[j] = tmp; } } } return 0; }
struct AdvTeacher* creatTArray(int count) { int i = 0; struct AdvTeacher*p1 = (struct AdvTeacher *)malloc(count*sizeof(struct AdvTeacher )); if (p1 == NULL) { return NULL; } for (i=0; i<count; i++) { memset(&p1[i], 0, sizeof(struct AdvTeacher)); memset(p1+i, 0, sizeof(struct AdvTeacher)); p1[i].a_name = (char *)malloc(128*sizeof(char)) ;// char buf[128*4] memset(p1[i].a_name , 0, 128*sizeof(char)); } return p1; } void freeTArray(struct AdvTeacher* tArray, int num) { int i = 0;
if (tArray == NULL) { return ; }
for (i=0; i<num; i++) { char *p = tArray[i].a_name; //为什么这里需要释放 if (p != NULL) { free(p); }
//辅导的时候,如果谁犯这个错误,拿10元买水果。立字 if (tArray[i].name != NULL) //这个地方要怎么做?错误,不能单独释放结构体中某一个内存单元,因为是整体申请的 { free(tArray[i].name); } }
if (tArray != NULL) { free(tArray); tArray = NULL; //垃圾话语 } }
void main() { int i = 0; struct AdvTeacher *pArray = creatTArray(4); if (pArray == NULL) { return ; }
// { // //p // char *p = (char *)malloc(100); // free(p+1); // }
for (i=0; i<4; i++) { int tmp = 0;
char *p = pArray[i].a_name;
printf("\n请输入age: " ); scanf("%d", &(pArray[i].age) ) ;
printf("\n请输入名字: " ); scanf("%s", pArray[i].name);
printf("\n请输入别名: " ); scanf("%s", p); }
printf("\nsorted before\n"); printtArray(pArray, 4 );
printf("\nsorted after\n"); sorttArray(pArray, 4 ); printtArray(pArray, 4 );
freeTArray(pArray, 4);
system("pause"); } |
结构体使用分析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.