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';
}
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';
}
相关文章推荐
- v4l(video for Linux)使用摄像头实验的一点小感慨
- 关于C语言字符串函数使用的一点心得
- 只要你的答案让我明白了如和使用Microsoft VisualC++6 但是我一点基础也没有。所以向大家请教 我想学习C语言 加分不是问题 还望得到大家的帮助 我一定把分数加到你满意为止
- 谢谢 C语言初级入门用哪个软件使用一点
- 在学习使用delphi进行网络时,书上忽略的一点东西
- 如何在linux下使用c语言操作临时文件
- C语言, 值得终身使用的编程工具(一)
- Asp.net中使用javascrip的模态窗体的一点体会。
- Transform 使用的一点心得。
- 深入了解C语言(函数的参数传递和函数使用参数的方法)
- Delphi程序使用Chm帮助的一点修改
- 在ASP.NET使用javascript的一点小技巧
- C语言, 值得终身使用的编程工具(三)――――使用建议
- java和struts使用中的一点总结--关于写在页面上的select域的选项选中后保存状态的办法!
- 在ASP.NET使用javascript的一点小技巧
- MSDN中“演练:使用 Web 窗体页创建分页的数据访问” 一文中的代码的一点改进
- C语言, 值得终身使用的编程工具(二)―――― “秘密”
- 我学习使用java的一点体会(8)
- Process使用的一点心得
- c#中使用MessageBox的一点不便