0

Pointer [HELP]

#include <stdio.h> int main() { int num = 10; int * ptr = NULL; int *pt = &num; ptr = &num; printf("Address of num = %x\n", &num); printf("Value of num = %d\n", num); printf("\n"); printf("Address of ptr = %x\n", &ptr); printf("Value of ptr = %x\n", ptr); printf("Value pointed by ptr = %d\n", *ptr); printf("\n"); printf("Address of pt = %x\n", &pt); printf("Value of pt = %x\n", pt); printf("Value pointed by pt = %d\n", *pt); return 0; } what's the difference between storing address of a value to a pointer while declaring pointer and after declaring pointer? After declaring pointer why can't i assign ,like: int *ptr = NULL; *ptr = &num ? what's the difference between say, &ptr, and ptr? Does ptr store an address and value temporary and it's real address &ptr(?) doesn't change?

17th Mar 2019, 3:00 PM
Tincture
1 Resposta
+ 1
Tincture haven't gone through your entire code, but quick check gives me some hint to post... *ptr = &num is illegal ... why ? because &num is address of num variable which is of type pointer. so, ptr = & num will work due to ptr is of type pointer... now coming to your point of *ptr = &num... *ptr is no more pointer... it represents value pointed by pointer variable... so, there you can assign value like *ptr = 10 but ptr can't have assigned value of type int... regarding int* ptr = &num ; is declaration and definition in a single line... int* ptr; // declaration ptr = &num; // definition both are same... just a different way...
17th Mar 2019, 3:53 PM
Ketan Lalcheta
Ketan Lalcheta - avatar