今天面試,C的傳值,好久都沒有看C了,很多C的都忘記了,筆試題做的不好!
#include<stdio.h>
void fun(int **p){
*p = (int *)malloc(sizeof(int));
**p =100;
printf("22 0x%x, 0x%x, 0x%x, 0x%x\r\n", (int)&p, (int)p, *p, (int)**p);
}
void main(){
printf("ffffff\r\n");
int *p=NULL;
printf("11 0x%x, 0x%x\r\n", (int)p, &p);
fun(&p);
printf("33 0x%x, 0x%x, 0x%x\r\n", (int)p, *p, (int)&p);
if(p!=NULL){
free(p);
}
}
運行的結果如下:
gs@ubuntu:~/01_learn/c$ ./testpointer
ffffff
11 0x0, 0x24c3aa10
22 0x24c3a9e8, 0x24c3aa10, 0x1fd4420, 0x64
33 0x1fd4420, 100, 0x24c3aa10