次の作業を行うにはどうすればよいですか?アイデアは、関数が外部ポインターを割り当てて、この概念を別のプログラムで使用できるようにすることですが、引数が互換性のないポインター型からのものであるとgccが言い続けるため、それはできません...単純なはずです、しかし、私はそれを見ていません。
#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;
}