指针的指针,以及指针传递
2022-11-15 22:40 钟铧若岩 阅读(34) 评论(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 }