Friday, July 17, 2020

Gentle Introduction to C : Pointers

Originally Posted on SeedBx


Welcome to the fifth part of the Gentle Introduction to C series. As said before this series aims to provide you a brief introduction to C language.

In this part we will be talking about one of the powerful features of C, pointers.


Before starting with pointers, let’s first start by looking at how data is actually stored in the computer memory.

Representation of Variables in Memory

For every variable defined, at the run-time, the compiler assigns/allocates a particular unit of memory (depending on the type of the variable) to the variable which can be uniquely identified with the help of an address id. The address simply specifies the location where a particular variable is stored in memory. If you like, you can even think of the computer memory as a huge array in which the address is simply the index of the position at which the variable is stored.


Note : The allocation behavior is totally compiler dependent, and therefore certain irregularities might be experienced across various compilers usually due to the fact that different compilers may use different techniques to optimise working.

Pointers

The primary job of pointers is to store addresses of other variables or memory location. Each pointer has a particular data type which just specifies the type of data to which the pointer will point to. However there is some uniqueness about void data type.

For example, a typical definition of a pointer will look like,

datatype *var_name;

The asterisk (*) there specifies that the variable defined is a not a normal variable and rather it is a pointer.

The pointer is basically intended to do two operations, namely access the address of a variable and store it, which is done using the ‘address of’ unary operator & (ampersand) and to access the value stored at the location which the pointer points to which can be done using the ‘at address’ unary operator * (asterisk). The latter operation is also called Dereferencing.


Note : The dereferencing operator * and the asterisk (*) at the declaration should not be confused with each other. It is just a typical situation of overloading of an operator and the two operators have no relation among them.


& Operator

The & operator is used to access the address of a variable.

Example -

          int *ptr;

          ptr=&a;

In the given example, the address of the variable ‘a’ (assumed to be declared somewhere before) is stored in the pointer variable ‘ptr’.

* Operator

The * operator is used to access the value stored at the address which the pointer points to.

Example -

          int *ptr;

          ptr=&a;

          int b;

          b=*ptr;

In the given example, the value of the variable ‘a’ is actually assigned to the variable ‘b’ using the pointer variable ‘ptr’.

Illustration specifying the basic working of Pointers

Note : The size of a pointer variable is typically 4 bytes or 8 bytes. The size of a pointer variable is compiler dependent and it often varies from one compiler to the other.

At last, pointers are a pretty powerful tool to manipulate and play with the addresses of different variables in order to optimise various workings and often handle dynamically defined structures.

 

Thanks for reading.

As usual your suggestions and feedback are always welcome.


Also See : Header Files, Functions, Call by Value and Reference


2 comments: