Page 101 - 6437
P. 101
To check for a null pointer, you can use an ‘if’ statement as follows:
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */
Pointers in Detail
Pointers have many but easy concepts and they are very important to C programming. The
following important pointer concepts should be clear to any C programmer:
Concept Description
Pointer arithmetic There are four arithmetic operators that can
be used in pointers: ++, --, +, -
Array of pointers You can define arrays to hold a number of
pointers.
Pointer to pointer C allows you to have pointer on a pointer
and so on.
Passing pointers to functions in C Passing an argument by reference or by
address enable the passed argument to be changed
in the calling function by the called function.
Return pointer from functions in C allows a function to return a pointer to
C the local variable, static variable, and dynamically
allocated memory as well.
Pointer Arithmetic
A pointer in C is an address, which is a numeric value. Therefore, you can perform
arithmetic operations on a pointer just as you can on a numeric value. There are four arithmetic
operators that can be used on pointers: ++, --, +, and
-
To understand pointer arithmetic, let us consider that ptr is an integer pointer which
points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic
operation on the pointer:
10
4