如何进行以下工作?这个想法是为函数分配一个外部指针,所以我可以在另一个程序中使用这个概念,但我不能这样做,因为gcc一直告诉我,参数来自不兼容的指针类型...它应该很简单,但我没有看到它。C:接收指针指针的函数,因此可以分配外部指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int allocMyPtr(char *textToCopy, char **returnPtr) {
char *ptr=NULL;
int size=strlen(textToCopy)+1;
int count;
ptr=malloc(sizeof(char)*(size));
if(NULL!=ptr) {
for(count=0;count<size;count++) {
ptr[count]=textToCopy[count];
}
*returnPtr = ptr;
return 1;
} else {
return 0;
}
}
int main(void) {
char text[]="Hello World\n";
char *string;
if(allocMyPtr(text,string)) {
strcpy(string,text);
printf(string);
} else {
printf("out of memory\n");
return EXIT_FAILURE;
}
free(string);
return EXIT_SUCCESS;
}
'allocMyPtr(text,&string)' – Roddy
忘记主要复制“文本”到“字符串”,这是不必要的行 – davi5e