下面是一些psudo,但我正在努力做到这一点。问题如所写,它返回一个空白指针。
int testFunction(char *t) {
int size = 100;
t = malloc(100 + 1);
t = <do a bunch of stuff to assign a value>;
return size;
}
int runIt() {
char *str = 0;
int str_size = 0;
str_size = testFunction(str);
<at this point, str is blank and unmodified, what's wrong?>
free(str);
return 0;
}
如果我有预定义的大小,例如char str[100] = ""并且我不尝试 malloc 或释放内存后缀,这会很好。不过,我需要能够使尺寸动态化。
我也试过这个,但似乎以某种方式遇到了损坏的指针。
int testFunction(char **t) {
int size = 100;
t = malloc(100 + 1);
t = <do a bunch of stuff to assign a value>;
return size;
}
int runIt() {
char *str = 0;
int str_size = 0;
str_size = testFunction(&str);
<at this point, str is blank and unmodified, what's wrong?>
free(str);
return 0;
}
谢谢!