c语言使用中的一点感慨

来源:互联网 发布:超图软件 新浪财经 编辑:程序博客网 时间:2024/04/30 01:02

typedef struct  _struct_test{

int a;

int b;

}struct_test;

正确使用二级指针方式:

void test_fuction(struct_test **pp);

int main(int argc,  char *argv[]){

struct_test *p=0;

test_fuction(&p);

printf("%d, %d", p->a, p->b);

free(p);

return 0;

}


错误使用二级指针方式(能达到使用的目的,但是会多消耗空间,多消耗时间.我第一眼看到错误的使用方式时,就懵了,居然将错误的程序完全否定,后来经过和原开发者沟通,发现,这段程序可以使用,只不过有缺陷。所以使用他人开发的程序,最好能取得代码使用demo,参照着使用。下一步就是分析程序是否有时间,空间上的缺陷;如果有,可以尝试修复):

void test_fuction(struct_test **pp);

int main(int argc,  char *argv[]){

struct_test **pp=malloc(sizeof(struct_test ));

test_fuction(pp);

printf("%d, %d", (*pp)->a, (*pp)->b);

free(*pp);

free(pp);

return 0;

}


void test_fuction(struct_test **pp){

*pp=(struct_test *)malloc(sizeof(struct_test ));

memset(*pp, 0, sizeof(struct_test ));

(*pp)->a='a';

(*pp)->b='b';

}



0 0