Change a pointer
来源:互联网 发布:网络博客行业 编辑:程序博客网 时间:2024/06/06 16:43
If you want to change something that's pass in as a parameter,you have to pass in a pointer to whatever you want to change. If it's a pointer,then you need to pass in a pointer to pointer(maybe someone will call it "double pointer ").
Now i’ll show you guys a case:
#include<malloc.h>#include<stdio.h>#include<stdlib.h>#include<string.h>void getmemory(char *p){ p=(char*)malloc(100); strcpy(p,"hello world");}int main(){ char *str = NULL; getmemory(str); printf("%s\n",str); free(str); return 0;}
this code can be compiled successfully,run it, it get corruption.
So how can I get this code to work? Here is the solution.
#include<malloc.h>#include<stdio.h>#include<stdlib.h>#include<string.h>void getmemory(char **p){ *p=(char*)malloc(100); strcpy(*p,"hello world");}int main(){ char *str = NULL; getmemory(&str); printf("%s\n",str); free(str); return 0;}
And its output: hello world
阅读全文
0 0
- Change a pointer
- 批量处理change pointer 生成IDOC
- 配置产生change pointer 表和字段
- IDoc-变更自动运行-change pointer 。
- A example of pointer
- Dereferencing a pointer
- Change a subject! please!
- Make a change
- a change of clothes
- A failed job change
- A New Change Problem
- A New Change Problem
- A wrong example of pointer
- the pointer to a structure
- the type of a pointer
- a trap in pointer moving
- a far address VS a far pointer
- attempt to dereference a generic a pointer
- 将Tomcat注册成服务
- ACPI _DSM
- web页面html中常用的特殊符号大全-可以直接用到页面里
- 深度学习笔记——深度学习框架TensorFlow(五)[TensorFlow大规模线性模型教程]
- WSDL实例解析
- Change a pointer
- 【math】7、输入一个数字,倒着输出
- 生成本地zip压缩包工具类
- UML关系明确解释
- HDFS文件导出本地合并为一个文件
- Block基本用法
- 322. Coin Change
- 责任链模式综述(基础篇)
- import和require