How to play with pointers in C

Donotalo
321.9K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

C has a type of variable which can store address or memory location. This type of variable is called a pointer. Essentially a pointer holds address of a variable. To declare a variable as a pointer, * is used before the variable name as seen in the example below:

A pointer is also called reference type variable in generic way. int *pa = &a; can be stated like pa is a pointer to an integer. pa points to a. Just like a has a content (55), pa has a content (that's the address of a). As pa itself is a variable, it has an address too.

The content of a can be obtained using pa. This is called dereferencing a pointer. Dereferencing a pointer can be performed by a * character before the pointer variable name as seen in the example below:

As you can see, *pa == a. Because pa == &a, we can also write *(&a) to get the content of a.

The declaration and assignment of a pointer can take place in different statements:

int *pa;
pa = &a;

It will produce the same result as the example before it.

Warning: It is dangerous to leave a pointer without pointing anything. This kind of pointer is called dangling pointer. Reading or dereferencing a dangling pointer results in undefined behaviour:

int *pa;

/* Fatal: reading uninitialized variable - undefined behaviour */
/* printf("The content of pa is %p\n", pa); */

/* Fatal: dereferencing dangling pointer - undefined behaviour */
/* printf("The dereferenced value of pa is %d\n", *pa); */

An undefined behaviour means that when a compiler encounters anything that triggers undefined behaviour, it is allowed to do anything it seems appropriate. For maximum portability of your program, make sure to avoid any undefined behaviour.

Since pointers are themselves variables, there can be pointer which points to another pointer! Run and observe the output of the following example:

In words:

Referencing/pointing

address of a  == content of pa
address of pa == content of ppa

Dereferencing

dereferenced value of ppa   == content of pa
dereferenced value of pa    == content of a
double dereferencing of ppa == content of a
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content