您的位置:首页 > 其它

结构体基础 数组 做函数参数

2015-02-01 11:57 127 查看
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

//两个结构体变量之间的copy行为
struct Teacher
{
char name[62];//64
int age ; //4
};

//两个结构体变量之间可以copy数据,这个是c编译器给我提供的行为,我们要顺从
void main81()
{
//告诉编译器要分配内存
//第一种方法
struct  Teacher t1 = {"dddd", 40};
struct  Teacher t2 = {"t2", 50};
struct  Teacher *  p = NULL;
t2 = t1; //这个是=号操作,不是初始化
p = &t2;

printf("sizeof(t1): %d\n", sizeof(t1));
printf("%s\n", t2.name);
printf("%s\n", p->name);

//用memcpy函数完成两个结构体变量之间的copy
memcpy(&t2, &t1, sizeof(struct  Teacher));

printf("%s\n", t2.name);
printf("%s\n", p->name);

//我们自己手工的域赋值
//copyObj01(t1, t2);

system("pause");
}

void copyObj01(struct  Teacher from, struct  Teacher to)
{
memcpy(&to, &from, sizeof(struct  Teacher));
}

void  copyObj02(struct  Teacher *from, struct  Teacher *to)
{
memcpy(to, from, sizeof(struct  Teacher));
}

//两个结构体变量之间可以copy数据,这个是c编译器给我提供的行为,我们要顺从
void main01()
{
//告诉编译器要分配内存
//第一种方法
struct  Teacher t1 = {"dddd", 40};
struct  Teacher t2 = {"t2", 50};
//int a ;
//结构体类型是复杂类型,结构体指针出来,就是一级指针
struct  Teacher *  p = NULL;

//我们自己手工的域赋值
copyObj01(t1, t2);
copyObj02(&t1, &t2);

printf("%s\n", t2.name);
printf("%s\n", p->name);

system("pause");
}

struct  Teacher *  creatTArray(int count)
{
int i = 0;
struct  Teacher*p1 = (struct  Teacher *)malloc(count*sizeof(struct  Teacher ));
if (p1 == NULL)
{
return NULL;
}
for (i=0; i<count; i++)
{
memset(&p1[i], 0, sizeof(struct  Teacher));
memset(p1+i, 0, sizeof(struct  Teacher));
}
return p1;
}

void main7()
{
//告诉编译器要分配内存
//第一种方法
struct  Teacher tArray[10];
struct  Teacher *  pa= NULL;
tArray[0].age = 10;

creatTArray(2);
pa[0].age = 10;

system("pause");
}


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct  Teacher_
{
char name[62];//64
int age;
};
int  printArray(struct Teacher_ * pArray,int num)
{
int i=0;
for(i=0;i<num;i++)
{
printf("%d\n",pArray[i].age);
}
return 0;
}
int sortArray(struct Teacher_ *pArray,int num)
{
int i=0,j=0;
struct Teacher_ tmp;
for(i=0;i<num;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;
}
}
printf("%d\n",pArray[i].age);
}
return 0;
}
void main()
{
int i=0;
struct  Teacher_ tArray[10];
for(i=0;i<4;i++)
{
int tmp=0;
printf("请输入age:");
scanf("%d",&tArray[i].age);
}
printArray(tArray,4);
sortArray(tArray,4);
system("pause");
}


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

//两个结构体变量之间的copy行为
struct Teacher
{
char name[62];//64
int age ; //4
};

int printtArray(struct Teacher *pArray, int num)
{
int i = 0;
for (i=0; i<num; i++)
{
printf("%d ", pArray[i].age);
}
return 0;
}

int sorttArray(struct Teacher *pArray, int num )
{
int i = 0, j = 0;
struct Teacher 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 Teacher *  creatTArray(int count)
{
int i = 0;
struct  Teacher*p1 = (struct  Teacher *)malloc(count*sizeof(struct  Teacher ));
if (p1 == NULL)
{
return NULL;
}
for (i=0; i<count; i++)
{
memset(&p1[i], 0, sizeof(struct  Teacher));
memset(p1+i, 0, sizeof(struct  Teacher));
}
return p1;
}

void main()
{
int i = 0;
struct Teacher *pArray  = creatTArray(4);
if (pArray == NULL)
{
return ;
}

for (i=0; i<4; i++)
{
int tmp = 0;
printf("\n请输入age: " );
scanf("%d", &(pArray[i].age));
}

printf("\nsorted before\n");
printtArray(pArray, 4 );

printf("\nsorted after\n");
sorttArray(pArray, 4 );
printtArray(pArray, 4 );

system("pause");
}


进阶2

#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");
}


综合

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

//两个结构体变量之间的copy行为
struct AdvAdvTeacher
{
char name[64];//64
char *a_name; //4 //结构体里面套一级指针
int age ; //4
char **  stuname;//
};

int printtArray(struct AdvAdvTeacher *pArray, int num)
{
int i = 0;
int j = 0;
for (i=0; i<num; i++)
{
printf("\n %d ", pArray[i].age);
printf("%s ", pArray[i].name);
printf("%s ", pArray[i].a_name);

for (j=0; j<3; j++)
{
char **p2 = pArray[i].stuname;
printf("%s ", p2[j]);
}
}
return 0;
}

int sorttArray(struct AdvAdvTeacher *pArray, int num )
{
int i = 0, j = 0;
struct AdvAdvTeacher 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 AdvAdvTeacher*  creatTArray(int count)
{
int i = 0;int j = 0;
struct  AdvAdvTeacher*p1 = (struct  AdvAdvTeacher *)malloc(count*sizeof(struct  AdvAdvTeacher ));
if (p1 == NULL)
{
return NULL;
}
for (i=0; i<count; i++)
{
memset(&p1[i], 0, sizeof(struct  AdvAdvTeacher));
memset(p1+i, 0, sizeof(struct  AdvAdvTeacher));
p1[i].a_name = (char *)malloc(128*sizeof(char)) ;// char buf[128*4]
memset(p1[i].a_name , 0, 128*sizeof(char));
//p1[i].stuname = (char **)malloc(100);

{
char **p2 = (char **)malloc(3*sizeof(char *));
for (j=0; j<3; j++)
{
p2[j] =(char *) malloc(128*sizeof(char));
memset(p2[j] , 0, 128*sizeof(char));
}
p1[i].stuname = p2;
}
}
return p1;
}

void free2p(char **p ,int num)
{
int i = 0; int j = 0;
for (i=0; i<num; i++)
{
if (p[i] != NULL)
{
free(p[i] );
}
}
free(p);
}
void freeTArray(struct AdvAdvTeacher* 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[i].stuname != NULL)
{
free2p(tArray[i].stuname, 3);
}
}

if (tArray != NULL)
{
free(tArray);
tArray = NULL; //垃圾话语
}
}

void main()
{
int i = 0; int j = 0;
struct AdvAdvTeacher *pArray  = creatTArray(4);
if (pArray == NULL)
{
return ;
}

// 	{
// 		//p
// 		char *p = (char *)malloc(100);
// 		free(p+1);
// 	}

for (i=0; i<2; 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);

for (j=0; j<3; j++)
{
char **p2 = pArray[i].stuname;
printf("\n请输入学生的名字");
scanf("%s", p2[j]);
}
}

printf("\nsorted before\n");
printtArray(pArray, 2 );

printf("\nsorted after\n");
sorttArray(pArray, 2 );
printtArray(pArray, 2 );

freeTArray(pArray, 2);

system("pause");
}


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

//两个结构体变量之间的copy行为
struct AdvAdvTeacher
{
char name[64];//64
char *a_name; //4 //结构体里面套一级指针
int age ; //4
};

struct AdvAdvTeacher*  creatTArray(int count)
{
int i = 0;int j = 0;
struct  AdvAdvTeacher*p1 = (struct  AdvAdvTeacher *)malloc(count*sizeof(struct  AdvAdvTeacher ));
if (p1 == NULL)
{
return NULL;
}
for (i=0; i<count; i++)
{
memset(&p1[i], 0, sizeof(struct  AdvAdvTeacher));
p1[i].a_name = (char *)malloc(128*sizeof(char)) ;// char buf[128*4]
memset(p1[i].a_name , 0, 128*sizeof(char));
//p1[i].stuname = (char **)malloc(100);
}
return p1;
}
void freeTArray(struct AdvAdvTeacher* 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);
}
}

if (tArray != NULL)
{
free(tArray);
tArray = NULL; //垃圾话语
}
}

//深copy 浅copy
void  copyObj03(struct  AdvAdvTeacher *from, struct  AdvAdvTeacher *to)
{
memcpy(to, from, sizeof(struct  AdvAdvTeacher));
to->a_name = (char *)malloc(128);
strcpy(to->a_name, from->a_name);
}

void main01()
{
//struct AdvAdvTeacher t1;
//struct AdvAdvTeacher t2;

struct AdvAdvTeacher*  p1 =creatTArray(1);
struct AdvAdvTeacher*  p2 =creatTArray(1);

printf("\n请输入age: " );
//scanf("%d", &(p1->age) ) ;
p1->age = 11;

printf("\n请输入名字: " );
//scanf("%s", p1->name);
strcpy(p1->name, "11");

printf("\n请输入别名: " );
//scanf("%s", p1->a_name);
strcpy(p1->a_name ,"2");

(*p2) = (*p1);

freeTArray(p1, 1);
freeTArray(p2, 1);
system("pause");
}

void main()
{
struct AdvAdvTeacher t1;
struct AdvAdvTeacher t2;
t1.age = 11;
t1.a_name = (char *)malloc(100);
strcpy(t1.a_name, "t1111");

// 	{
// 		char *p = (char *)malloc(100);
// 		free(p);
// 		free(p);
// 	}

//t2 = t1; //编译器赋值操作 编译器等号=操作
copyObj03(&t1, &t2);
if (t1.a_name != NULL)
free(t1.a_name);

if (t2.a_name != NULL)
free(t2.a_name);
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐