指针的指针,以及指针传递
2022-11-15 22:40 钟铧若岩 阅读(37) 评论(0) 编辑 收藏 举报
1 #include <cstddef> 2 #include <cstdlib> 3 #include <iostream> 4 #include "string.h" 5 using namespace std; 6 7 8 void GetMemory(char **p,int num) 9 { 10 *p = (char *)malloc(sizeof(char)*num); 11 cout << "&p = " << p << endl; 12 13 } 14 char *GetMemory(char *p,int num) 15 { 16 p = (char *)malloc(sizeof(char)*num); 17 return p; 18 } 19 int main() 20 { 21 char *a = NULL; 22 GetMemory(&a, 10);//传了一个指向指针的指针, 23 strcpy(a,"helloworld"); 24 cout << "&a = " << &a << endl; 25 cout << "a = " << a << endl; 26 cout << "*a = "<< *a <<endl; 27 28 char *b = NULL; 29 b = GetMemory(b,100); 30 strcpy(b, "hello"); 31 cout << "b = " << b << endl; 32 33 return 0; 34 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类