surprise how how it will happen!!!!!!!!

Status
Not open for further replies.

grittinjames

Advanced Member level 1
Joined
Jun 1, 2006
Messages
479
Helped
44
Reputation
90
Reaction score
32
Trophy points
1,308
Location
bangalore india
Activity points
3,985
hai
just comment about this problem in C
int *p=NULL;
*p= 10;
printf("%d",*p);



printed answer is 10;


in a null pointer where this 10 is stored

plz tell me
 

You need to give the pointer a valid address to point to, before using it.

int x=3;

int* p1; // p1, p2 have a random addresses
int* p2;

*p1=NULL; // writing zero (NULL) to an undefined address
// the content of the unknown address (p1) (that might be used by another variable) is corrupted

*p1=10; // writing 10 to the same undefined address
printf("%d", *p1); // it will print 10 of course; 10 was stored somewhere in the memory

// but here:
p2=&x; // p2 takes an address that we know (x's address)
*p2=3; // writing to the address we want
printf("%d",*p2); // prints 3
 

Code:
int  *p=NULL;
This assigns a value (probably zero) to p and declares p to be a pointer to an integer value. The storage pointed to by p now begins at address location 0 and includes the next few contiguous locations as required to store an integer. For example, if your integer size is 32 bits, p is now a reference to address locations 0 through 3.

Code:
*p= 10;
This stores the integer value 10 at address locations 0-3 (assuming 32 bit integers), thus overwriting whatever was previously stored in these locations. At this point, you have probably just trashed storage that is being used for some other purpose.

Code:
printf("%d",*p);
This fetches and displays the contents of locations 0-3.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…