指针传递
来源:互联网 发布:绿色上网软件手机软件 编辑:程序博客网 时间:2024/06/06 03:17
错误程序--程序崩溃:
#include <stdlib.h>#include <string.h>#include <stdio.h>void GetMemory(char* p){p = (char *)malloc(100);}void main(){ char* str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str);}
修改:
方法1:函数参数改为引用
#include <stdlib.h>#include <string.h>#include <stdio.h>void GetMemory(char* &p){p = (char *)malloc(100);}void main(){ char* str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str);}方法2:传递二级指针
void GetMemory(char** p){ *p = (char *)malloc(100);}int main(void){ char* str = NULL; GetMemory (&str); strcpy(str, "hello world/n"); printf(str); return 0;}
0 0
- 指针传递
- 指针传递
- 指针传递
- 指针传递 引用传递
- 指针传递,引用传递
- 传递指针和传递指针的指针
- 参数传递之指针传递
- 指针传递和引用传递
- 指针传递 引用传递 小结
- 指针传递和引用传递
- 值传递和指针传递
- 参数传递之指针传递
- 指针传递和引用传递
- 参数传递之指针传递
- c++ 值传递 引用传递 指针传递
- 什么是值传递、引用传递、指针传递
- 传递变量名,传递指针,传递引用
- 值传递、指针传递和引用传递
- 2010-02-04 11:55 图片与文字垂直居中
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- 区域生长opencv2下的实现
- 扩展欧几里德定理
- 【DP&数组】Best Time to Buy and Sell Stock
- 指针传递
- 使用Struts2开发Java Web应用程序(目录)
- vs2012使用命令行编译的设置
- Github error – fatal: Unable to look up github.com (port 9418) (Name or service not known)
- RT-Thread基础一
- 快速排序算法
- STM32F4 串口程序 调试记录 hardfault_handle 问题
- 优化Python程序性能的15个小技巧
- Oracle创建表设置自动增长列范例