次のコードは受け入れられますか。つまり、これは malloc を行う正しい方法ですか?
これは、私の状況で動作するようになる最小限のコードです。それは「正しい方法」だと思いますが、私はCが初めてで、全体的な手がかりがあまりありません。関連するSOの投稿をいくつか読みましたが、この状況と完全に一致するものはないようです。コメント?
#include <stdio.h>
// example of calling a function that creates a dynamically sized array and
// returns it to a caller that doesn't know or care about the size of the array
char* return_char_array(){
// for the sake of the example, we determined the size to be 100
char *f=malloc(100*sizeof(char));
// stick something in the first couple of elements for test purposes
*f=65;
*(f+1)=66;
return f;
}
int main(){
// we want this function to remain ignorant of the size or other workings
// of the array, so, no '[]' or 'malloc'
char *wipc = return_char_array();
// well i guess it cares a little, because we assume there are at least 2 elements...
printf("%c,%c\n",*(wipc),*(wipc+1));
system("PAUSE");
return 0;
}