Some doubts regarding NULL pointers

Guys, I have the following doubts regarding NULL pointers,

1. Can we have a pointer to a NULL pointer ?
2. What happens when a NULL pointer is dereferenced ?
3. I think this is possible. You can assign a NULL pointer to another pointer right ?
1. yes
2. usually some kind of segmentation fault or access violation type thing
3. yes.

A 'NULL' pointer is simply a pointer that contains the value 0 (or NULL) rather than a valid memory address.

In C++ Straustrup recommends using 0 rather than NULL. I think its clearer that way.

1
2
3
4
5
6
7
8
char* p1 = 0; // p1 ia a 'null' pointer because it has value 0

char* p2; // p2 is a 'dangling' pointer because its value is invalid.

char* p3 = p1; // p3 is a null pointer because it copied the 0 (NULL) value from p1

char** pp1 = &p1; // pp1 is a pointer to a null pointer because p1 happens to be null (0)
Topic archived. No new replies allowed.