- Ian Collins。 是的。 #include <stdlib.h>#include <stdio.h> void foo(double *ptr){printf("foo ptr=%p\n",ptr);ptr = malloc(sizeof(*ptr));printf("foo ptr=%p\n",ptr);ptr[0] = 12;printf("foo value=%f\n",*ptr);}int main(int argc, char **argv){double *ptr=NULL;printf("main ptr=%p\n",ptr);foo(ptr);printf("main ptr=%p\n",ptr);}Returns: main ptr=(nil)foo ptr=(nil)foo ptr=0x804a008foo value=12.000000main ptr=(nil) Why the pointer in the main function is not updated? Thnaks in advance. jamaj 解决方案 Because you are updating the local copy in foo. ptr in foo is copy ofthe one in main. You have to write void foo(double **ptr){*ptr = malloc(sizeof(*ptr));} --Ian Collins.Yes. --Ian Collins. 这篇关于由功能问题分配的指针......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 02:21