関数にポイントを割り当てられないのはなぜですか。次のコードで気づいたように。関数が戻った後、正しいアドレスを指すポインターp1を割り当てることができません。しかし、グローバルポインタ* pを使用すると、アドレス情報を格納できます。
#include <stdio.h>
#include <stdlib.h>
int *p = NULL;
void test(int * pt1, int**pt2){
p = (int*)malloc(sizeof(int));
pt1 = p;
*pt2 = p;
printf("p points to %p\n", p);
printf("pt1 points to %p\n", pt1);
printf("pt2 points to %p\n", *pt2);
}
int main(void) {
int *p1 = NULL;
int *p2 = NULL;
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
test(p1, &p2);
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
return 0;
}
出力:
p points to (nil)
p1 points to (nil)
p2 points to (nil)
p points to 0x8acb008
pt1 points to 0x8acb008
pt2 points to 0x8acb008
p points to 0x8acb008
p1 points to (nil)
p2 points to 0x8acb008