C Pointer
Learning pointers in C is both simple and interesting. With pointers, the execution of some C programming tasks can be simplified, and there are other tasks, such as dynamic memory allocation, that cannot be performed without pointers. Therefore, to become a good C programmer, it is necessary to learn pointers.
As you know, every variable has a memory location, and every memory location defines an address that can be accessed using the & operator, which represents an address in memory.
Look at the following example, which will output the addresses of the defined variables:
Example
#include
<
stdio.h
int
main
(
)
{
int
var_runoob
=
10;
int
p
;
// Define pointer variables
p
= &
var_runoob
;
printf
(
"
Address of the variable var_runoob: %p
\
n
"
,
p
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
var_runoob 变量的地址: 0x7ffeeaae08d8

Through the example above, we understand what a memory address is and how to access it. Next, let us look at what a pointer is.
What is a Pointer?
A pointer is also a memory address, and a pointer variable is a variable used to store a memory address. Just like other variables or constants, you must declare a pointer before using it to store the address of another variable. The general form of a pointer variable declaration is:
type *var_name;
Here,type is the base type of the pointer, which must be a valid C data type,var_name is the name of the pointer variable. The asterisk * used to declare a pointer is the same as the one used in multiplication. However, in this statement, the asterisk is used to specify that a variable is a pointer. The following are valid pointer declarations:
int
ip
;
/*
a pointer to an integer
*/
double
dp
;
/*
a pointer to a double
*/
float
fp
;
/*
a pointer to a float
*/
char
ch
;
/*
a pointer to a char
*/
All actual data types, whether integer, floating-point, character, or other data types, have the same type of corresponding pointer values, all being a long hexadecimal number representing a memory address.
The only difference between pointers of different data types is the data type of the variable or constant the pointer points to.
How to use pointers?
The following operations are frequently performed when using pointers: defining a pointer variable, assigning the address of a variable to the pointer, and accessing the value at the address available in the pointer variable. These are done by using the unary operator * to return the value of the variable located at the address specified by the operand. The following example involves these operations:
Example
#include
<
stdio.h
int
main
(
)
{
int
var
=
20;
/*
Declaration of the actual variable
*/
int
ip
;
/*
Declaration of the pointer variable
*/
ip
= &
var
;
/*
Store the address of var in the pointer variable
*/
printf
(
"
Address of the variable var: %p
\
n
"
, &
var
)
;
/*
The address stored in the pointer variable
*/
printf
(
"
Address stored in the variable ip: %p
\
n
"
,
ip
)
;
/*
Access the value using the pointer
*/
printf
(
"
Value of the variable *ip: %d
\
n
"
, *
ip
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
var 变量的地址: 0x7ffeeef168d8
ip 变量存储的地址: 0x7ffeeef168d8
*ip variable value: 20
NULL pointers in C
When declaring a variable, if there is no exact address to assign, it is good programming practice to assign a NULL value to the pointer variable. A pointer assigned a NULL value is called anullpointer.
A NULL pointer is a constant defined in the standard library with a value of zero. See the program below:
Example
#include
<
stdio.h
int
main
(
)
{
int
ptr
=
NULL;
printf
(
"
The address of ptr is %p
\
n
"
,
ptr
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
ptr 的地址是 0x0
On most operating systems, programs are not allowed to access memory at address 0, because that memory is reserved by the operating system. However, memory address 0 has special significance: it indicates that the pointer does not point to an accessible memory location. By convention, if a pointer holds a null value (zero), it is assumed to point to nothing.
To check for a null pointer, you can use an if statement, as shown below:
if
(
ptr
)
/*
If p is not NULL, then done
*/
if
(
!
ptr
)
/*
If p is NULL, then done
*/
C Pointers in Detail
In C, there are many pointer-related concepts; these concepts are all simple but very important. The following lists some important pointer-related concepts that C programmers must be clear about:
| Concept | Description |
|---|---|
| Pointer arithmetic | Four arithmetic operations can be performed on pointers: ++, --, +, |
| Arrays of pointers | You can define arrays used to store pointers. |
| Pointers to pointers | C allows pointers to pointers. |
| Passing pointers to functions | Passing parameters by reference or address allows the passed parameters to be changed in the called function. |
| Returning pointers from functions | C allows functions to return pointers to local variables, static variables, and dynamically allocated memory. |