您的位置:首页 > 编程语言 > C语言/C++

c语言使用中的一点感慨

2014-09-05 23:00 204 查看
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';

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: