代码改变世界

指针的指针,以及指针传递

  钟铧若岩  阅读(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 }
复制代码

 

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示