C 语言函数值传递和指针传递区别

2025-09-30 14:17:45

1、#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",str); 

free(str); 

return 0; 

}

并且每次调用函数,都会泄露100大小内存空间

C 语言函数值传递和指针传递区别

2、#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; 

C 语言函数值传递和指针传递区别

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢