dereferencing

int *p,*v;
*p=5;
cout<<*p;
this program crashes at run time
whereas

int *p,*t,i=5;
p=&i;
*t=*p;
cout<<*t;
runs
please tell me why?
i am using dev c++ ide
Last edited on
In the first one, you're never reserving any memory for p to point to, nor are you pointing it to memory you already have.

Line 2 in the program that crashes is like pointing to an empty space or to a bookshelf and saying "paint that car blue". You need to remember that computers have no imagination. ;)

-Albatross
thanks it's really helpfull
Topic archived. No new replies allowed.