|
|
|
|
|
|
|
#include <stddef.h>
.
.
.
intPtr = NULL; |
|
|
|
|
|
|
|
|
As with any named constant, the identifier NULL makes a program more selfdocumenting. Its use also reduces the chance of confusing the null pointer with the integer constant 0. |
|
|
|
|
|
|
|
|
It is an error to dereference the null pointer, as it does not point to anything. The null pointer is used only as a special value that a program can test for: |
|
|
|
|
|
|
|
|
if (intPtr == NULL)
DoSomething(); |
|
|
|
|
|
|
|
|
We'll see examples of using the null pointer later in this chapter and in Chapter 18. |
|
|
|
|
|
|
|
|
Although 0 is the only literal constant of pointer type, there is another pointer expression that is considered to be a constant pointer expression: an array name without any index brackets. The value of this expression is the base address (the address of the first element) of the array. Given the declarations |
|
|
|
|
|
|
|
|
has exactly the same effect as |
|
|
|
|
|
|
|
|
Both of these statements store the base address of arr into ptr. |
|
|
|
|
|
|
|
|
Although we did not explain it at the time, you have already used the fact that an array name without brackets is a pointer expression. Consider the following code, which calls a ZeroOut function to zero out an array whose size is given as the second parameter: |
|
|
|
|
|