Pointers in c language are the most important tool. It plays a vital role in c language. You cannot be perfect C programmer without the knowledge of pointers. When you write a program than some task only performed by the pointers.

What is a pointer in c?

A pointer is similar to a variable but the difference is that pointers are stored the address of a location in memory and variable stored the value.In other words, we can say, a pointer is used to reference a location in the memory.

When we have used a pointer to store the address in the memory than using the dereferencing techniques we can also get the value from the address which is stored by the pointer.

Note: Value of the pointer is a memory address.



Syntax of pointer

Declaration of a pointer is very important because at the time of declaration you define the capability of pointers. Every pointer has the data types and name followed by an asterisk (*) operator.

Data_Type  * Pointer_Name;

Let’s take an example to understand the declaration of a pointer.

int *iPtr;

In above expression, I am declaring a pointer to an integer to store the address of integer variable.

How do a pointer work in C?

A pointer is a most important topic in c language but it is difficult to understand the concept of a pointer. I feel that before going in depth first we need to understand the concept of lvalue and rvalue.

Whenever in the program we have declared a variable then we need to inform the compiler two things, one is the data type (int, char, and float) and the second thing is the name of the variable. The compiler assigns the storage to the variable as per their data type.

         DATA TYPE                         SIZE ( in Byte)
            char                           1
            short                           2
            int                          2 or 4
            long-long                           8
            float                           4
            double                           8

After the declaration of a variable compiler assign a unique address to the variable and attached this unique address with the variable name. In a program using the variable name, we can store and retrieve the value from the address which is assigned to the variable.

For example, let’s declare a variable which data type is int and name iPtr.

int iPtr;

When the compiler sees “int” then it sets 2 or 4 bytes (as per the size of integer) of memory to hold the integer value and it also sets up a symbol table. In that table, it adds the symbol iPtr and the relative address in memory where those 2 or 4 bytes were set aside.

After the declaration of iPtr, now we are able to store the value.

iPtr = 10;

When the above expression is executed, 10 is stored in the memory location reserved for the iPtr.

pointers in c

So we can say there is two property attached with variable, its value (integer value), and its address (location where integer value stored).
Here, the integer value is rvalue and location where the integer value is stored called lvalue.

Let’s take an example,

 

Note: lvalue can be rvalue but rvalue never become an lvalue in c language.

Now I think you have the little understanding of lvalue and rvalue. So let’s come on the topic.

So in c language pointer is used to store address (lvalue) of a location (variable) in memory. When the compiler sees the asterisk (*) symbol with the identifier then it sets a byte of memory which is required to hold the address (lvalue) of a variable.

Let’s take an example,

 

Important:  When the compiler sees ‘&’ with the variable name then fetch the address (lvalue ) of the variable.

Let see the below image for the better understanding.

understanding and using c pointers

Note:  When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it. So generally at the time of declaration of a pointer, we initialized the pointer with a NULL pointer.



Dereferencing of pointers in c

When we have used dereference (*) operator with lvalue then it goes to the address and accesses the data from the memory for the manipulation. In simple word, the dereference (*) operator looks up the value that exists at the address.

Let’s see an example code,

In below c program example, I have created an integer variable and integer pointer. The created integer pointer point the address of the created variable. Here using the dereference operator we can change the value of the variable.

Arithmetic operation on a pointer

We can also use the arithmetic operators with pointers there is some exception like the multiplication and division operators.

I have listed some arithmetic operators in the below list which is used with pointers.

Indirection (*) This operator is used to get the value from the pointed address.
Reference operator( &) This operator is used get the address of the variable or pointer.
Assignment ( =) You can assign the value to the pointer or value to the address which is pointed by the pointer.
Incrementing You can add integer value to the pointer to point the different memory location.
Decrementing You can subtract the integer value from the pointer to point the different memory location.
comparison This operation is valid only between two pointers that point to the same array.

When we increment or decrement the pointer then pointer increase or decrease a block of memory (block of memory dependent on the pointer type).

So for the character pointer when we increment pointer then it incremented 1 byte of the address.

Let’s see the below example for the character and float pointer.

char *pcData =NULL;

pointer arithmetic

If we increment the float pointer then it incremented 4 bytes of address.

float *pfData = NULL;

 

pointer arithmetics

In C language, we can also add the integer value to the pointer. So if we added 2 to the float pointer then float pointer moves 2 float positions i.e 8 bytes.

float  *pfData = NULL;

pfData  = pfData  + 2;

pointers meaning

Like the float pointer if we have added 2 to the character pointer then character pointer move 2 character location i.e 2 bytes.

char *pcData = NULL;

pcData = pcData + 2;

understanding pointers in c

 





Pointer and array

In C-language pointer and array are very close to each other. Actually, the name of the array is a pointer to its first element. So if aiData is an array of integer then “aiData” will be the address of its first element. You can also say that “aiData” is similar to the &aiData [0].

Let’s see a program example

Relationship between pointer and array

Array elements stored in a consecutive memory block we can access the elements of the array using the indexing but with the help of pointers, we can also access the elements of an array.

Consider the following example.

Let suppose acBuffer is an array of character.
char acBuffer [ ] = {‘a’, ’t’, ’i’, ’c’ ,’l’ ,’e’};
So acBuffer[0] is the 1st element of the array like that acBuffer[i] is the ith element of the array.

Note: We can also write an array in terms of the pointer, see the below expression.

*(acBuffer+ i) = acBuffer[i];
(acBuffer+ i) = &acBuffer[i];

To access the element of the array through the pointer create a pointer.

char * pcBuffer = NULL;

pcBuffer = acBuffer; // character pointer point the address of first element
Now using the pointer you can access all elements of the array.

For the clarity see the below picture 

 

Note: If acBuffer is an array of character then &acBuffer[i] is the pointer to the character and its value is the address of the ith element of the acBuffer.

Difference between pointer and array

Here is one important difference between array and pointer is that address of the element in an array is always fixed we cannot modify the address at execution time but in the case of pointer we can change the address of the pointer as per the requirement.

Consider the below example:

In below example when trying to increment the address of the array then we will get the compiler error.

array using pointer

Note: When an array is passed to a function then it decays its pointer to the first element.

Pointer and function

A pointer to function is similar to the other pointers but the only difference is that it points to a function instead of the variable. In the other word, we can say, a function pointer is a type of pointer that store the address of a function and these pointed function can be invoked by function pointer in a program whenever required.

Declaration of function pointer in c

The syntax for declaring function pointer is very straight forward. It seems like difficult in beginning but once you are familiar with function pointer then it becomes easy.

The declaration of a pointer to function is similar to the declaration of a function. That means function pointer also requires a return type, declaration name, and argument list. One thing that you need to remember here is, whenever you declare the function pointer in the program then declaration name is preceded by the * (Asterisk) symbol and enclosed in parenthesis.

For example,

void ( *fpData )( int );

For the better understanding, let’s take an example to describe the declaration of a function pointer in c.
e.g,
void ( *pfDisplayMessage) (const char *);

In above expression, pfDisplayMessage is a pointer to a function taking one argument, const char *, and returns void.

When we declare a pointer to function in c then there is a lot of importance of the bracket. If in the above example, I remove the bracket, then the meaning of the above expression will be changed and it becomes void *pfDisplayMessage (const char *). It is a declaration of a function which takes the const character pointer as arguments and returns void pointer.

List of some function pointers

A pointer to function must have the same signature to the function that it is pointing to. In a simple word, we can say that function pointer and its pointed function should be same in parameters list and return type.

So there can be a lot of possibility of function pointer in c. In below section, I am listing some function pointers and I want you to write the name of these function pointers in a comment box.

For more detail see this link: Pointer to function



Pointer and Structure

We can also use the pointer with user defined data type like structure and union. In structure, pointer use to link the nodes. We can also create a pointer to structure in c language when we need to pass the huge structure variable to a function.

Let see below example for the better understanding

See below article for more detail:

How to use the structure of function pointer in c language?

Function pointer in structure

Common pointer mistakes

Here I am mentioning some mistake which is generally done by the programmer.

Uninitialized pointer

It occurs when programmer not assigning a valid address to the pointer before using it. When we try to give the value to an uninitialized pointer then we will not get compiler error but when running the code then we will get run time error.

Uninitialized pointer behavior is totally unpredictable that’s some people called it the wild pointer.

Let see the below example:
When we will try to run below code then code will be crashed.

Pointer comparison

It occurred when you are tried to compare the pointer which is pointing to the different memory object.

See the below example,

 

Note: I have already discussed at the beginning of the article we can only compare the pointer if they are pointing the same array.

Ilegal indirection

In C language malloc function request for the memory block and return the beginning address of the allocated memory block.Sometimes we forget to remove the asterisk (*) symbol when assigning the address to the pointer.

Let’s see the below example

But mistake in below expression.

In above statement, asterisk creates the problem. When we removed the asterisk then above statement become valid.

Pointer pointing invalid address

When pointer pointing an address which is not available and you try to assign the value to the address then you will get the run time error (segmentation fault). It is a very common mistake which is generally made by the programmer.

See the below example
When you compile the below code it compiles perfectly but when you try to run the compiled code then you will get the run time error because you have already free the allocated memory and try to assign the data.

Example code 1

Example code 2