Skip to main content

C Pointers

C Pointer

Pointer is a variable which are store the address of other variables. if you are learn c programming so pointer are create an important role. because without pointers there are not possible to utilize dynamic memory allocation of heap area.

Pointer operator

There are two type of operators as follows.

1) Address of operator (&)

We are know about that variable they are store the value in particular location of memory. can i get this location address? ans is yes. Get the address by using (&) operator ("address of operator").

Address of operator

Look at this example.

/*
  Example of & operator in pointers
*/
#include<stdio.h>

int main(){
  
  int num=10;

  printf("\nValue of num : %d",num);//print value
  printf("\nAddress of num: %p",&num);//print address 
  return 0;
}

Output :

Value of num : 10
Address of num: 0x7ffd349f20c4

in this example num is integer variable there are storing the value of 10. when we print num then it will print value. but we are use front of & operator then it will print address of num (address of variable). Display address using (%p) format specifiers.

2) De-referencing operator (*)

Get the value at location. If we are assign variable address to pointer. then help of pointer we are utilize assign address value.

de referencing operator

For example

/*
  Basic pointer example
*/
#include<stdio.h>

int main(){
  
  int num=10; //integer variable
  int *ptr; //pointer variable

  ptr=&num ;//assign address

  printf("\nValue of num : %d",num);//print value
  printf("\nAddress of num: %p",&num);//print address
  printf("\nValue of ptr :%p",ptr);//print num address
  printf("\nAddress of ptr :%p",&ptr);//printed your address
  printf("\nvalue at address (*ptr) : %d",*ptr);//print value of num
  return 0;
}
pointer explanation

Output

Value of num : 10
Address of num: 0x7fff72ef246c
Value of ptr :0x7fff72ef246c
Address of ptr :0x7fff72ef2470
value at address (*ptr) : 10  

ptr value is address of num variable. so how to get value of this address. solution are using of a dereference (*) operator.

understand this point using this example.

/*
  Basic pointer example
*/
#include<stdio.h>

int main(){
  
  int num=10; //integer variable
    printf("\nvalue of num : %d",*&num);//print value of num
  return 0;
}

Output

value of num : 10

(&num) address of num variable. and *(&num) value of num variable. similarly (*&num) are return value of num.

How to use pointers

Three steps to use pointer variables.

How to use Pointer

1) Define

We know about that before utilize variable must be declared. similarly pointer also a variable then its should be declared or defined first.

Syntax

data-type * variable-name;

Explanation

Data type: Allowed to use all data type of here in c language. example int, char float etc.

Variable name: Append (*) in front of variable name. that is represent as pointer variable.

Examples

  int *auxiliary; //auxiliary are an integer pointer
  float *data; //here data are an float pointer
  char *info; //here info are an char pointer

Other example: structure pointer are also possible.

  //structure of Node
  struct Node{
    int data; 
  };
  //struct pointer ptr1 and ptr2
  struct Node*ptr1,*ptr2;

Example of global pointers

/*
  Example of global pointer
*/
#include<stdio.h>
/*Global pointers*/
int *counter;
float *salary;
char *status;
int main(){
  /*Print address of given pointer*/
  printf("\nAddress of counter pointer : %p",&counter);
  printf("\nAddress of salary pointer : %p",&salary);
  printf("\nAddress of status pointer : %p",&status);
  
  /*print value of given pointer*/
  printf("\nValue of counter pointer : %p",counter);
  printf("\nValue of salary pointer : %p",salary);
  printf("\nValue of status pointer : %p",status);
  return 0;
}

Output

Address of counter pointer : 0x601050
Address of salary pointer : 0x601040
Address of status pointer : 0x601048
Value of counter pointer : (nil)
Value of salary pointer : (nil)
Value of status pointer : (nil)

in this program we are try to print address of global pointer. pointer are variable which are store the address. so we are try to print value of pointer(address). so it will print (nil). basically global pointer by default assign the value of NULL. and GCC compiler are print (nil). some other compiler also print 0x00000000 or 0.

Conclusion is global variable default value are NULL. and NULL contain 0. Best practices assign the value of pointer when it's declare. if we are not have value then Assign NULL value.

2) Assign

pointer value is address. so assign the address of variable by using &(using ampersand) operator.

Syntax

pointer_variable=& variable_name;

For example

/*
  Example of pointer
*/
#include<stdio.h>

int main(){
  int *ptr=NULL;// initial ptr is NULL
  int data=10;
  ptr=&data ;//assign address
  printf("\n *ptr : %d",*ptr);// print value of data
  return 0 ;
}

Output

*ptr : 10

3) Access

utilize the pointer value.

/*
  Example of pointer in c
*/
//include header file
#include<stdio.h>
//Function declaration
void swap(int *,int *);
/*
    Function definition
    name: swap
    parameters: 2 Integer pointer
    return : none
    work : swapping given integer pointer value
*/
void swap(int *ptr1,int *ptr2){
    //temp variable store *ptr1 value
    int temp=*ptr1;
    //replace value
    *ptr1=*ptr2; 
    *ptr2=temp;
}
int main(){
    //integer variable
  int no1=10,no2=20;
    
    printf("\nBefore swap no1 : %d & no2 : %d",no1,no2);
    swap(&no1,&no2);// passing address of no1 and no2
    printf("\nAfter swap no1 : %d & no2 : %d",no1,no2);

    return 0;
}
assign pointer address

Output:

Before swap no1 : 10 & no2 : 20
After swap no1 : 20 & no2 : 10

Pointers in Detail

1) Pointer arithmetic

Pointer arithmetic operator

Four arithmetic operator are used in pointer

1) Increment ++
2) Decrement --
3) -
4) +

1) increment ++

/*
  Example of pointer arithmetic (++)
*/
#include<stdio.h>

int main(){
    /*Array elements*/
  char data[]="CODE";

    //get size of array
  int size=(sizeof(data))/sizeof(data[0])
     ,index=0;
  char *data_ptr=data; //assign base address of array

  for(index;index<size;index++){
    printf(" %c",*data_ptr); //print data value
    data_ptr++; //increment address by one
  }
  
  return 0;
}

Output:

 C O D E

Explain: initial data_ptr assigned the address of array. assume that this address are 100.

increment address using pointer

After increment pointer data_ptr ++. it will point to next address of array (101).

increment address using pointer set-1

After increment pointer data_ptr ++. it will point to next address of array (102).

increment address using pointer set-2

After increment pointer data_ptr ++. it will point to next address of array (103).;

increment address using pointer set-3

Note that increment (++) operator are used to pointer . when assigning the address of array to pointer. because array are contiguous memory allocation and in this case increment pointer variable (++) then it will move or point to next memory block of array.

Look at this example of increment pointer variable which are point to another variable.

/*
  Example of pointer arithmetic (++) in variable 
*/
#include<stdio.h>

int main(){
    int data=5;
    int *ptr=&data ;
    printf("\n Address of data : %p",&data);
    printf("\n Address of ptr %p",&ptr);

    printf("\n Value of ptr %p: ",ptr);
    printf("\n (*ptr) value at address : %d",*ptr);
    /*note that increment pointer value. 
    but this is integer pointer then 
    (32 bit compiler are integer size is 4 bytes) 
    it will increment 4 bytes.*/
    printf("\n\n After increment\n");
    ptr++;
    printf("\n Value of ptr %p",ptr);//print value of pointer

    //print garbage value
    printf("\n (*ptr) value at address : %d",*ptr);

  return 0;
}
Before Increment Pointer

After Increment Pointer

After Increment Pointer

Output:

 Address of data : 0x7fffa69045ec
 Address of ptr 0x7fffa69045f0
 Value of ptr 0x7fffa69045ec: 
 (*ptr) value at address : 5

 After increment

 Value of ptr 0x7fffa69045f0
 (*ptr) value at address : -16778304

-16778304 is garbage value

2) Decrements (--)

Example

/*
  Example of pointer arithmetic (--)  
*/
#include<stdio.h>

int main(){
  //array elements
    int data[]={1,2,3,4};
    //size of array
    int size=(sizeof(data)/sizeof(data[0])),
    *int_ptr=data+(size-1);// assign address
    //same as
    //*int_ptr=&data[size-1];
    while(size>0){
      printf(" %d",*int_ptr);
      int_ptr--; //decrement pointer 
      size--;//decrement size variable
    }
  return 0;
}

Output

pointer decrement example
 4 3 2 1

3) Minus (-)

Example

/*
  Example of pointer arithmetic (-)  
*/
#include<stdio.h>

int main(){
  //array elements
    int data[]={1,2,3,4};
    //size of array
    int size=(sizeof(data)/sizeof(data[0])),
    *int_ptr=data;// assign address
    //perform subtraction of two pointers
    printf("Distance is %ld ",data - data);
    printf("\nDistance is %ld ",(data+3)- (data+1));
    
    return 0;
}

Output

Distance is 0 
Distance is 2 

4) Plush (+)

/*
  Example of pointer arithmetic (+)  
*/
#include<stdio.h>

int main(){
  //array elements
    int data[]={10,20,30,40},index=0;
    //size of array
    int size=(sizeof(data)/sizeof(data[0])),
    *int_ptr=data;// assign address
     
    for(index;index<size;index++){
        // *(int_ptr + index)
      printf(" %d",*(int_ptr+index));
      //same as 
      //printf(" %d",int_ptr[index]);
      
    }

    return 0;
}

Output:

 10 20 30 40

note that subtract address (example Assume that data initial 100 and (data+3) = 103 or data+1 =101). here 103-101 = 2.

2) Array of pointers

Example

/*
  Example of Pointer to array 
*/
//include header file
#include<stdio.h>

int main(){
   int arr[]={8,7,6,1},length=4,i=0,j=0;
   int *ptr[4];

    for(i=0;i<length;i++){
        ptr[i]=arr;//initialize address
    }
    for(i=0;i<length;i++){
      printf("\nptr[%d]",i);
      for(j=0;j<length;j++){
        //print pointer array value
        printf("\n ptr[%d][%d] : %d",i,j,ptr[i][j]);
      }

   }
    return 0;
}
pointer array

Output

ptr[0]
 ptr[0][0] : 8
 ptr[0][1] : 7
 ptr[0][2] : 6
 ptr[0][3] : 1
ptr[1]
 ptr[1][0] : 8
 ptr[1][1] : 7
 ptr[1][2] : 6
 ptr[1][3] : 1
ptr[2]
 ptr[2][0] : 8
 ptr[2][1] : 7
 ptr[2][2] : 6
 ptr[2][3] : 1
ptr[3]
 ptr[3][0] : 8
 ptr[3][1] : 7
 ptr[3][2] : 6
 ptr[3][3] : 1

Pointer to pointer

Pointer to pointer means one pointer is store the address of another pointers. this is also called multiple indirection. Note that if we are store address of another pointer then append extra * asterisk.

Example

/*
  Example of Pointer to pointer
*/
//include header file
#include<stdio.h>

int main(){
   int data=11;// normal integer variable
   int *ptr1=&data ; //single pointer
   int **ptr2=&ptr1 ; //double pointer
   //print data value
   printf("\n value of data : %d",data);
   printf("\n value of *ptr : %d",*ptr1);
   printf("\n value of **ptr: %d",**ptr2);
    return 0;
}
Example Pointer to pointer

Output

 value of data : 11
 value of *ptr : 11
 value of **ptr: 11

There are no limit to use number of asterisk append before pointer variables.

For example

/*
  Example of Pointer to pointer
*/
//include header file
#include<stdio.h>

int main(){
    
    int number=786;
    int *ptr1=&number,
        **ptr2=&ptr1,
        ***ptr3=&ptr2,
        ****ptr4=&ptr3,
        *****ptr5=&ptr4 ;

    printf("\n Value of *****ptr5 : %d",*****ptr5);
    printf("\n Value of ****ptr4 : %d",****ptr4);
    printf("\n Value of ***ptr3 : %d",***ptr3);
    printf("\n Value of **ptr2 : %d",**ptr2);
    printf("\n Value of *ptr1 : %d",*ptr1);
        
    return 0;
}
Example Pointer to pointer
 Value of *****ptr5 : 786
 Value of ****ptr4 : 786
 Value of ***ptr3 : 786
 Value of **ptr2 : 786
 Value of *ptr1 : 786

Passing pointers to functions

Example

/* 
Passing pointers to functions in c
*/
#include<stdio.h>

/*Function declaration*/
void printArray(int *,int);

/*Print array element*/
void printArray(int *arr,int size){
    printf("\n Array elements \n");
    int i=0;
    for(i;i<size;i++){
        printf("\narr[%d] : %d",i,arr[i]);
    }
}
int main(){
    //array elements
    int data[]={1,2,3,4,5};
    //size of array
    int size=sizeof(data)/sizeof(data[0]);
    int *ptr=data;//assign array address
    printArray(ptr,size);//passing pointer 
    return 0;

}
passing pointer

Output

 Array elements 

arr[0] : 1
arr[1] : 2
arr[2] : 3
arr[3] : 4
arr[4] : 5

Function pointer example

Function pointer is also called subroutine pointer or procedure pointer. Example of function pointer

/* 
Example of function pointer
*/
#include<stdio.h>

void message(int,int);
void message(int value1,int value2){
    printf("\n %d  %d",value1,value2);
}

int main(){
    /*ptr is a pointer to function*/
    void (*ptr)(int,int)=&message ;
    /*Call function pointer*/
    ptr(1,2);
    (*ptr)(1,2);
    return 0;
}
 1  2
 1  2
Function pointer example

Properties of pointers

1) Static and global pointer are NULL.

2) size of pointer depends system architecture. if you are use 32 bit compiler then size of pointer is 4 bytes. and if you are use 64 bit compiler then size of pointer are 8 bytes.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment