charへのポインタへのポインタを使用しているときにこの問題に遭遇しました:
void setmemory(char** p, int num)
{
*p=(char*)malloc(num);
}
void test(void)
{
char* str=NULL;
setmemory(&str,100);
strcpy(str,"hello");
printf(str);
}
int main()
{
test();
return 0;
}
上記のコードは正しいですが、なぜここでポインターchar ** pへのポインターを使用するのか理解できませんか?代わりにcharへのポインタを使用するのはなぜですか?だから私はこのスニペットを以下に変更し、それが機能していないことがわかりました、誰かが理由を教えてもらえますか?ありがとう!
void setmemory(char* p, int num)
{
p=(char*)malloc(num);
}
void test(void)
{
char* str=NULL;
setmemory(str,100);
strcpy(str,"hello");
printf(str);
}
int main()
{
test();
return 0;
}