Introduction to Programming and C++

Contents Previous Topic Next Topic

In C, the null pointer (a default value for a pointer to nothing) was represented by NULL. I suspect that this was to allow different platforms to define NULL as appropriate.

The C++ Standard says that the appropriate value for the default pointer is the zero address 0.

If you use NULL, you must either define it yourself, or you may have included it with another package. For example, NULL is defined in the iostream header file, so Program 1 will work, but Program 2 will not compile.

Program 1. Using NULL after including iostream.

#include <iostream>

using namespace std;

int main() {
	int * ptr = NULL;

	return 0;
}

Program 2. Invalid use of NULL.

int main() {
	int * ptr = NULL;

	return 0;
}

Questions

1. What kind of error message do you get if you try to assign to NULL after having included the iostream header? Can you guess if NULL was defined using #define NULL 0 or using const void * NULL = 0;?


Contents Previous Topic Top Next Topic