C Pointers

C Pointers are the variables whose value is the address of another variables.
Pointer Syntax :

data_type *var_name;

Example :

int *p; char *p;

Here, * is used to denote that the pointer variable is not a normal variable.

We can easily perform some C programming tasks with the help of C Pointers such as dynamic memory allocation, which cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let’s start learning them in simple and easy steps.


Declaration of C Pointers:

The pointer in c language can be declared using * (asterisk symbol).

int *variable_1; // pointer to int
char *variable_2; // pointer to char
float *variable_3; // pointer to float

A simple example of C Program using C Pointer:
Here is the simple example of a pointer to find the address of variable.

#include       
 int main()
{      
int x=10;  
int *p; 
// & symbol is used to get the address of the variable.     
p=&number; 
printf("Address of x variable is %d \n",&number);  
printf("Address of x variable is %d \n",p);  
printf("Value of x variable is %d \n",*p);  
return 0;      
}

NULL Pointer

At the time of declaration, if we don’t have any address to be specified in the pointer then we can assign NULL value. When a pointer is not assigned any value, it is known as a NULL pointer.
A pointer that is not assigned any value but NULL is int *p=NULL;
In most of the libraries, the value of the pointer is 0 (zero).


Some Important points about pointer:

  • We must declare a pointer before using it to store any variable address.
  • The pointer variable might be belonging to any of the data types such as int, float, char, double, short, etc.
  • A normal variable stores the value whereas pointer variable stores the address of the variable.
  • Value of C pointer (address) always is a whole number.
  • Firstly, C pointer is initialized to null always, i.e. int *p = null. The value of the null pointer is 0. If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • symbol is used to get the value of the variable that the pointer is pointing to.
  • Two pointers can be subtracted to know how many elements are available between these two pointers. But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for the 16-bit compiler).

C Pointer to Pointer

With the help of pointer to pointer variable, we can store the address of a pointer variable. This address point to the value of a variable.
General Format of the pointer to pointer variable:

data_type **p2;

Example of pointer to pointer variable:
Let’s see the example of a pointer to pointer variable. In this example, we have assigned a value in the variable. We have defined the pointer variable, which stores the address of that variable. We have also defined a pointer to pointer variable which stores the address of the pointer variable(value of the variable).

#include         
int main()
{        
int variable=100;    
int *p1;//pointer to int  
int **p2;//pointer to pointer      
    
p1=&variable;//stores the address of x variable    
p2=&p1;  
        
printf("Address of x variable is %d \n",&variable);    
printf("Address of p variable is %d \n",p1);    
printf("Value of *p variable is %d \n",*p1);    
printf("Address of p2 variable is %d \n",p2);    
printf("Value of **p2 variable is %d \n",**p1);    
    
return 0;       
}

C Pointer Arithmetic

C Pointer holds the address of a value. That’s why we can perform some arithmetic operations on the pointer variable. Here is the list of commonly used C Pointer Arithematic :

  • Increment
  • Decrement
  • Addition
  • Subtraction
  • Comparison

Examples of C Pointers Arithmetic:

Addition of pointer:

Let’s see an example of adding some value in a pointer-type variable.
Description:
We have assigned some value in a variable, while the address is stored in a pointer-type variable. Then we have added some value in the pointer-type variable.

#include         
int main()
{          
int variable=10;      
int *pointer; // pointer to int    
pointer=&variable; // stores the address of number variable      
printf("Address of pointer variable is %u \n",pointer);  
pointer=pointer+45;     
printf("\nAfter adding 10 in pointer value of x=%d \n",pointer); 
return 0;
}

Subtraction of pointer:

Let’s see an example of subtracting some value from a pointer-type variable.
Description:
We have assigned some value in a variable, while the address is stored in a pointer-type variable. Then we have subtracted some value from the pointer-type variable.

#include         
int main()
{          
int variable=10;      
int *pointer; // pointer to int    
pointer=&variable; // stores the address of number variable      
printf("Address of pointer variable is %u \n",pointer);  
pointer=pointer-5;
printf("\nAfter subtracting 5 from pointer value of x=%d \n",pointer); 
return 0;
}

Increment of pointer:

Let’s see an example of increment in the value of a pointer-type variable.
Description:
We have assigned some value in a variable, while we have stored their address in a pointer-type variable. Then we have done the pre-increment in the value of a pointer-type variable.

#include         
int main()
{          
int variable=10;      
int *pointer; // pointer to int    
pointer=&variable; // stores the address of number variable      
printf("Address of pointer variable is %u \n",pointer);  
pointer=++pointer;
printf("\nAfter increment value of pointer variable is=%d \n",pointer); 
return 0;
}

Decrement of pointer:

Let’s see an example of decrement in the value of a pointer-type variable.
Description:
We have assigned some value in a variable, while we have stored their address in a pointer-type variable. Then we have done the pre-decrement in the value of a pointer-type variable.

#include<stdio.h>         
int main()
{          
int variable=10;      
int *pointer; // pointer to int    
pointer=&amp;variable; // stores the address of number variable      
printf("Address of pointer variable is %u \n",pointer);  
pointer=--pointer;
printf("\nAfter decrement value of pointer variable is=%d \n",pointer); 
return 0;
}

Let’s see an example of a comparison of two pointer-type variables.
Description:
We have assigned some value in two variables, while we have stored their address in two pointer-type variables. Then we have done the comparison between the two pointer-type variables.

<!-- wp:preformatted {"className":"lang:c"} -->
<pre class="wp-block-preformatted lang:c"><code>#include         
int main()
{          
int variable_1=10;
int variable_2=20;      
int *pointer_1,*pointer_2;     
pointer_1=&amp;variable_1; 
pointer_2=&amp;variable_2;
if(pointer_1&gt;pointer_2)
printf("address of variable 2 is greater than variable 1");
else
printf("address of variable 1 is greater than variable 2");
return 0;
}</code></pre>
<!-- /wp:preformatted -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.