指针参数的问题
来源:互联网 发布:魔兽传输数据遇到问题 编辑:程序博客网 时间:2024/04/29 11:56
#include <stdio.h>#include <stdlib.h>int f(int *ptr){ptr=(int *)malloc(sizeof(int));*ptr=999;return 0;}int main(){int *p,k=5;p=&k;f(p);printf("%d",*p);//5}
解决这个问题,有两种:
(一)利用函数返回值方法
#include <stdio.h>#include <stdlib.h>int *f(){int *ptr=(int*)malloc(sizeof(int));*ptr=999;return ptr;}int main(){int *p=f();printf("%d",*p);//999free(p);}(二)利用指向指针类型的指针
#include <stdio.h>#include <stdlib.h>void f(int **ptr){*ptr=(int *)malloc(sizeof(int));**ptr=999;}int main(){int *p=NULL;f(&p);printf("%d",*p);//999free(p);//避免内存泄漏}
摘自《C语言点滴》215页
- 指针参数的问题
- 指针以及指针做参数的问题
- 指针参数传递的问题
- 用指针传递函数参数的问题
- Hook KiUserExceptionDispatcher参数指针错误的问题
- 函数指针参数的传递问题
- 指针作为函数参数的问题
- 指针的传参数与赋值问题
- 指针作为函数参数的问题
- 函数指针参数的传递问题
- C++指针作为函数参数的问题
- 数组作为函数参数的指针问题
- 指针作为函数参数传递的问题
- 指针作为参数传递的相关问题
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)(转)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(指针的指针)(转)
- 帐套
- 10月13日百度笔试整理
- emacs 快捷键(包括C/C++模式)
- iOS 7: 如何为iPhone 5s编译64位应用
- C++代码风格
- 指针参数的问题
- java的代码风格
- oracle异常80004005
- Leetcode: Rotate Image
- 防止android service 被杀死
- 预处理命令“ # 运算符”和“ ## 运算符” "#@"运算符
- C# 操作 Word 修改word的高级属性中的自定义属性
- Holdout 验证
- android利用JNI调用C++自定义类