- #1
kidsasd987
- 143
- 4
int main(void)
{
int *ptr;
int a=1, b=2, c=3;
ptr = &a;
printf("%p\n", &ptr);
printf("%p\n", &b);
printf("%p\n", &c);
ptr = b;
printf("%p\n", ptr);
printf("%d\n", a);
system("PAUSE");
return 0;
}
ok here is the code I wrote.
ptr(the pointer that stores the address of integer a) is assigned the value 2, and now points the address 2.
therefore a value is still 1.
but somehow when I changed the red colored part to
*ptr=b, now a has a value of 2.
when I change *ptr=&b, a has a weird large value.
could you guys explain me why this happens?
{
int *ptr;
int a=1, b=2, c=3;
ptr = &a;
printf("%p\n", &ptr);
printf("%p\n", &b);
printf("%p\n", &c);
ptr = b;
printf("%p\n", ptr);
printf("%d\n", a);
system("PAUSE");
return 0;
}
ok here is the code I wrote.
ptr(the pointer that stores the address of integer a) is assigned the value 2, and now points the address 2.
therefore a value is still 1.
but somehow when I changed the red colored part to
*ptr=b, now a has a value of 2.
when I change *ptr=&b, a has a weird large value.
could you guys explain me why this happens?