Pointer In C: Different Ways To Use Pointers With Examples

Pointer can be use with any program where we are going to deal with memory locations. Let's check out some examples.

1. Arithmetic Operations With Pointer

With some complex problems where we may have to access different memory locations so that jumping from one memory location to another pointer arithmetic operations takes place.

Pointer can perform addition (+, pre and post ++) and subtraction(-, pre and post  --)  with memory locations.

Note: Programmer have to be very calculative with arithmetic operations otherwise pointer can point invalid memory location. This is the reason programmers prefer pointer arithmetic operations on array's memory locations because array allocates memory in contiguous manner.

Pointer memory address increments/decrements according the size of datatype.

Current_Address + ( incremented_by * size_of_datatype )

Current_Address - ( decremented_by * size_of_datatype )

Memory Allocation in pointer
Memory Allocation In Pointer. ByTechAchievers.com

In This example int A has 1001 memory address and pointer is incremented by 1 then next address would be 1005.




Similarly other memory address will be calculate according to their size of datatypes.  Let's understand more with examples.

#include <stdio.h>


void  main() {
  

 int A=4;

 int *ptr;
 
 ptr = &A;


//Using %u to print unsigned value


printf ("Address of A - %u\n",ptr);

printf("Pointer pointing to A address , value of A - %d \n", *ptr); 

ptr = ptr+1;

printf ("Now pointer pointing to new address - %u\n",ptr);
 
}

Output -

Address of A - 2906444796

Pointer pointing to A address , value of A - 4

Now pointer pointing to new address - 2906444800


      
Note: Pointer "ptr" memory address has changed and now its not pointing to "A".

We can also perform pointer arithmetic,comparisons between the same type of pointers as well. 

In the example we are dealing with memory address stored in pointer variable, if we write *ptr now it will work with value of the memory address stored in pointer i.e value of A.

#include <stdio.h>


void  main() {
  

 int A=4;

 int *ptr;
 
 ptr = &A;


//Using %u to print unsigned value

printf ("Address of A - %u\n",ptr);

printf("Pointer pointing to A address , value of A - %d \n", *ptr);

*ptr = *ptr+1;

printf ("Updating value of A - %d\n",A);

}

Output -

Address of A - 2785439084

Pointer pointing to A address , value of A - 4

Updating value of A - 5

2. Pointer to Pointer

Pointer can have pointer, pointer to pointer variable will hold address of pointer.
It will be denoted by double asterisk (**), same as for pointer to pointer to pointer will have three asterisk sign and so on, there is no limit of creating pointer to pointer.

The purpose of pointer to pointer is to increase the process of memory access,  and accuracy in the program.

It maintains indirection of pointers, all memory location will be indirectly connected. It a chain of pointers. 


pointer of pointer
Pointer to Pointer. ByTechAchievers.com
here B is the pointer of A and C is the pointer of B.


#include <stdio.h>


void main() {

  
  int A = 4;

  int *ptr1,**ptr2,***ptr3;


  ptr1 = &A;

  ptr2 = &ptr1;

  ptr3 = &ptr2;
  

  printf ("Value of A - %d\n ", A);

  printf ("Value of A via ptr1 - %d\n ", *ptr1);

  printf ("Value of A via ptr2 - %d\n ", **ptr2);
  
  printf ("Value of A via ptr3 - %d\n\n ", ***ptr3);


  printf ("Address of A - %u\n ", &A);
  
  printf ("Value of ptr1 i.e address of A - %u\n\n ", ptr1);
  

  printf ("Address of ptr1 - %u\n ", ptr1);
  
  printf ("Value of ptr2 i.e address of ptr1 - %u\n\n ", ptr1);

  
  printf ("Address of ptr2 - %u\n ", ptr2);
  
  printf ("Value of ptr3 i.e address of ptr2 - %u\n\n ", ptr1);

  
  printf ("Address of ptr3 - %u\n ", ptr3);
  
  
}

Output - 
 
 Value of A - 4
 
 Value of A via ptr1 - 4
 
 Value of A via ptr2 - 4
 
 Value of A via ptr3 - 4

 Address of A - 3435468828
 
 Value of ptr1 i.e address of A - 3435468828

 Address of ptr1 - 3435468828
 
 Value of ptr2 i.e address of ptr1 - 3435468828

 Address of ptr2 - 3435468816
 
 Value of ptr3 i.e address of ptr2 - 3435468828

 Address of ptr3 - 3435468808


3. Use Of Pointer With Array

As we know array is the collection of same datatype values and all values stores in contiguous manner. 

If there are 10 elements in array, 10 memory location will be allocated.
Each array index will have memory address, if we need to access 4th element of array then we have to specify the array index example : array[4].


char array in C.
Char Array .ByTechAchievers.com 










as you can see in the above example each index has its own memory address but what would be the address of array variable?

Answer is the first index represent the address of main array variable. Lets see the example to print address of array and then its indexes.



#include <stdio.h>


void main() {
  

  int array[3]={1,2,3};


  printf("Address of array - %u \n\n",&array);


  for( int i =0; i<=2; i++) {


    printf("Value - %d and Address of each index - %u\n\n ",array[i], &array[i]);

  }
  
}


Output -


 Address of array - 2895152420

 Value - 1  and Address of each index - 2895152420

 Value - 2  and Address of each index - 2895152424

 Value - 3  and Address of each index - 2895152428


In the above example address of array and address of 0th index of array are equal.

Now come to the pointer, pointer of array holds address of 0th index only.
to access other index's address pointer will be incremented.

Let's see another example how pointer works with array ...

#include <stdio.h>


void main() {
  

  int array[3]={1,2,3};

  int *ptr;


  ptr= &array;  // or ptr = &array[0] both are same


  printf("Address of array - %u \n\n",&array);

  printf("Value of pointer i.e. address of array - %u \n\n",ptr);

  printf ("Access value of array using pointer\n\n");


  for( int i =0; i<=2; i++) {


    printf("Value - %d  and Address of %d index - %u\n\n ",*ptr, i,ptr);


    ptr = ptr + 1; //memory address will be incremented by 4 bytes


  }
  
}


Output - 


Address of array - 2736994932

Value of pointer i.e. address of array - 2736994932

Access value of array using pointer

Value - 1  and Address of 0 index - 2736994932

Value - 2  and Address of 1 index - 2736994936

Value - 3  and Address of 2 index - 2736994940

4. Pointer With String

String is a collection of character array as we have already discussed about string and how it works C in previous chapter. How String Works In C

The first character's memory address is the pointer itself that is why we don't mention & operator with string variable. Let's understand via example .



#include <stdio.h>

# include <string.h>


int main() {
  

char name[] = "John";

char *ptr = name;


printf("String value - %s\n\n",name);


printf("Printing string using pointer - \n\n");


for (int i=0; i<strlen(name);i++) {

    printf("%c",*ptr);

    ptr++;

}
  
  return 0;

}


Output - 


String value - John

Printing string using pointer -John



5. Use Of Pointer With Functions (Passing/Returning)

Use of pointer with function i.e. implementing function as call be reference.

We pass pointer in function as parameter that is passing actual variable's memory location to function, any change done by function will impact to actual variable value. For more on function handling In C click on the link



Assume that we have a function to get the square of any number. We will pass a number to the function and function will return square of the number. 

Function does operation on number and then returns the result, but actual number will remain same because we send copy of the number not actual variable. This is called "Call by Value" and when we send actual variable i.e. the memory address of the variable to function then actual variable will get changed with new value. This is call "Call By Reference".

Default way of passing values to function is call by value and call by reference is achieved by pointer. For more understanding on pointer click on the link.

Pointer To Function / Call By Reference Example :



#include <stdio.h>


int getSquare (int *ptr);



void main() {


int num, square;


printf("Enter any number -\n");


scanf("%d",&num);


square = getSquare(&num);


printf("The actual number is - %d\n\n",num);


printf("The square of the number is - %d\n",square);


}


int getSquare (int *ptr){


  *ptr =  *ptr * *ptr;
  

  return *ptr;

}



6. Use Of Pointer With Structure

As we already know structure is the group of different datatypes. It makes programming easy we don't need to handle individual variables, structure variable represents all of them. Similarly one structure pointer would be able to make any change in any variable run-time.

For more details on structure please go through with the link. Structure In C


#include <stdio.h>


void main() {

  
struct employee {


char emp_name[20];

int emp_code;

float salary;

}e1;


struct employee *ptr;

ptr = &e1;


printf("Enter employee name\n");
scanf("%s",e1.emp_name);


printf("Enter employee code\n");
scanf("%d",&e1.emp_code);


printf("Enter employee salary\n");
scanf("%f",&e1.salary);


printf("Employee got 70% bonus of his salary\n");


ptr->salary =e1.salary + (e1.salary*70)/100; 


printf("Employee's updated salary ... %f ",e1.salary);

}

In this example you can see e1 is the structure variable containing char,int and float type values and ptr is the pointer of e1 so that it can make any change in any structure's variable. Here salary is calculated and it directly updated in its variable through pointer.

Structure variable denotes its variable through . and structure's pointer denotes via -> sign. 

I hope you like this article, Please share and comment.
-Thanks

No comments

Powered by Blogger.