Hello,
can someone tell me why or what of dereferenceing a pointer variable, which has the value NULL? I saw this in the book I was reading and could not find what it meant by that.
Printable View
Hello,
can someone tell me why or what of dereferenceing a pointer variable, which has the value NULL? I saw this in the book I was reading and could not find what it meant by that.
Dereferencing a NULL pointer results in a segmentation violation. It's not something you want to happen. ;) Run this and see how a segv occurs on your system:
-PreludeCode:#include <stdio.h>
int main ( void )
{
int *p = NULL;
*p = 10;
return 0;
}
A NULL pointer is a pointer that points to... "nothing". (NULL is defined as 0, or (void*)0 ).
Dereferencing a pointer means using the asterisk to get the value it points to.
Sorry for using cout... as that's C++.. but you get the picture :)Code:// ...
int *pointer;
int a = 7;
pointer = &a;
cout << *pointer << endl; // dereferences pointer and outputs 7
null pointing to zero is a compiler specific thingQuote:
Originally posted by rmullen3
A NULL pointer is a pointer that points to... "nothing". (NULL is defined as 0, or (void*)0 ).
Dereferencing a pointer means using the asterisk to get the value it points to.
Sorry for using cout... as that's C++.. but you get the picture :)Code:// ...
int *pointer;
int a = 7;
pointer = &a;
cout << *pointer << endl; // dereferences pointer and outputs 7
Thanks, Prelude, the program "vomitted".
(Like you knew it wouldn't!) :)
The 'type' is undefined, but may be re-defined later. Sans that, get the "barf bags" out!
-Skipper