Pointers For Dummies By a Dummy

Anush Somasundaram
3 min readAug 15, 2021
Points to X

Let’s imagine that you have a paper due on the various communication mediums that were used in the 1970’s. Now that’s a long time ago, and all you have as a source for information is an encyclopedia from 1982 , very stupid situation but it works. So you can either copy everything from the encyclopedia onto your paper or you can be smart, go to the index and find the pages that contain relevant information and write a summarised version of the information available.

The index page of the encyclopedia made it easier for you to locate the required information. The index page contains all the page numbers of locations of topics. These page numbers are nothing but addresses of topics. These addresses point to various parts of the encyclopedia. Without the index page of the encyclopedia, the information inside would not be accessible.

Similarly when we declare variables in code, the variables are stored in memory, and when memory is not indexed it can get real cluttered and confusing. So memory locations are given addresses to make them easily accessible. These addresses can be stored in variables called pointers. So technically a pointer is a variable that holds the address of another variable.

The textbook definition of a pointer is “a variable that stores a memory address”.

C has pointer operators, these are “&” and “ * ”.The “&” operator is called “the address off” operator, it is a unary operator that returns the memory address of a variable. The “ * ” operator is called the “indirection” operator, it is a unary operator that returns the value stored in a particular address of memory.

In the snippet of code below the “&” operator is shown in action.

“&” pointer operator in action

The output of the above snippet of code is:

Now let’s look at a snippet of code which initialises a pointer variable and shows how to use it.

Snippet of code to show pointers and pointer operators in action

On line 9 we initialise the pointer “pointer”, we also declare the pointer by giving it the value of the address of the variable x.

The syntax for initialisation of a pointer in C is “int *(name_of_pointer)”.

Essentially we have created a variable that holds the address of another variable.

Depiction a pointer variable in memory

On line 12 we print out the value of the pointer and also dereference it. Dereferencing a pointer is to obtain the value of the variable that is held in that particular location of memory.

The syntax to dereference a pointer is “ *(name of pointer) ”

The output obtained is:

Output of above snippet of code

Pointers are very useful when using variables across multiple functions. They make changing the values of variables very easy. Hopefully this helped you a little bit.

--

--

Anush Somasundaram

Looking for interesting software projects (ML/DL/NLP anything).