Pointer’s In Depth

Pointer’s are the variable that stores the address of another variable.

Whenever there is a need to refer/store the address of another variable for any operation, we use pointers.

Pointer variables can be declared as follows:

int *p; //p is a pointer to integer
char *a; //a is a pointer to character
float *z; //z is a pointer to float

Basics of Pointers

When using pointer’s, 2 operators are mainly used:

  • Address of Operator (&)
  • Value at Address Operator (*)

Address of operator (&):

The ‘&’ operator would return the address of memory location of a particular variable.

print(" %u ",&a); //it will print the address of a

Value at Address Operator (*):

The ‘*’ operator would return the value at the address of the memory location.

print(" %d ",*a) // it will print the value at address of a.

Behind Pointer’s

What is the necessity of using pointers?.

Why can’t we just use normal variables?.


As we know memory is a contiguous block of addresses. We use variables to refer to the memory location because remembering the addresses of a memory location is not easy for humans.


Whenever we declare variables, the OS maps that variable with a physical address in the memory, whichever is free.

Here’s where Pointer’s come into the picture. Pointer’s are a great tool to access memory locations at fast speeds. They provide direct access to the data present at memory locations.

Representation of Pointer’s

Say for Example:

If we write the below code:

int a=10; //a is integer variable which stores 10
int b=20; // b is integer variable which stores 20

After executing the above code, 10 would be stored at a memory address say 300,and 20 would be at 200.

Now,

We we write :-

int *p = &a; //pointer p storing address of a
int *q = &b; //pointer q storing address of b

After the above statements, the q will contain the address of b variable & p will have the address of variable a.

i.e q will point to a memory location where b is stored,and p will point to a location where a is stored.

When we write :-

print(" %d ",*p); //print value of b  10
print(" %d ",*q); //print value of a  20

What is the size of pointer variable ?

The pointer variable is constant in size(1 byte, 2 bytes depending on the machine ). i.e a character pointer can too hold the address of an integer variable, an integer variable can hold the address of float and so.


Since addresses are memory locations irrespective of the type of data that they hold. The pointer variable just needs to hold these memory addresses.
then, you might say, what is the use of specifying the data type for pointer variables?

You will get the answer when we refer to pointers for arrays below.

Significance of Using Pointer’s

To understand the significance of pointer’s.

Look at the code below,

void swap(int x , int y){
    int temp = x;
    x = y;
    y = temp;
}
void main(){
 int a = 10,b = 20;
 swap(a,b);
 printf("%d %d",a,b);
}

What would be the output of above code?.

You say 20 10 , No , It will be 10 20 .

Why didn’t the values get swapped?.

It was because,In C, the arguments to the function are passed by value & not by reference, thus, the swapping happened only inside the function swap(), leaving no changes to the passed variables.


So, what changes do I need to do to swap the variables ?.

Take a look at the code below:-

void swap(int *x , int *y){
    int temp = *x;
    *x = *y;
    *y = temp;
}
void main(){
 int a = 10,b = 20;
 swap(&a,&b);
 printf("%d %d",a,b);
}

We passed the address of variable a & b to function swap() instead of just value.


Which altered the data at their locations, leaving changes in the output.

Thus the output will be 20 10. (because variables were swapped when the address was specified).

Pointer’s for Arrays

To undestand how pointer’s work with arrays?.

Lets analyze the code below:

int main(){

  int arr[]={1,2,3,4,5,6}; //array definition
  int *p = arr; // pointer to array of integers
  printf("%d",*p); //prints arr[0]

}

In the above code,

We have declared a pointer to array of intergers,

The code *p = arr is actually assigning the base address of the array arr to pointer variable p.

Thus, p contains the address of array arr.

Now, printf("%d",*p) would print the first element of array arr i.e arr[0],because the p points to the base address of array arr.

What if we changed the above code to :-

int main(){

  int arr[]={1,2,3,4,5,6}; //array definition
  int *p = arr; // pointer to array of integers
 
  p++; //increment the p
  p++;

  printf("%d",*p); // it will print 3 i.e arr[2]
}

In the above code, we have incremented the pointer p , before printing the *p.

It will output 3.

When you access any element in a array,say arr[3] , the compiler translates the statement to

*( arr + 3 ) i.e print the 3rd element from the starting address.

How does the compiler know ,How many elements to skip ?

Because we have specifed that, p is a integer pointer.

Look here:

int * p;

Thus, whenever the p is incremented, it would be incremented as per the size of the data type,i.e integer here.


Hence it is mandatory to specify the type of pointers When we declare it.

Pointer’s Example

Lets’ understand the pointer’s with a sample code ,

Below is simple Linked-List data structure implementation:-

//include header files 
#include<stdio.h>
#include<stdlib.h>

//declare structure Node 
struct Node{
     int data;
     struct Node *next;
 };
 
struct Node *HEAD; 
//function declaration
struct Node create(int); 
void add(struct Node); 
void display();

//main 
 int main(){
     HEAD=NULL;
     add(create(10));
     add(create(20));
     add(create(30));
     add(create(40));
     add(create(50));
     add(create(60));
     add(create(70));
     display(); 
     return 0;
 }

//function definition
 struct Node *create(int x){
     struct Node *tmp=(struct Node*)malloc(sizeof(struct Node));
     tmp->data=x;
     tmp->next=NULL;
     return tmp;
 }

 void add(struct Node *node){
     if(HEAD==NULL){
         HEAD=node;
     }else{
         struct Node *trav=HEAD;
         while(trav->next!=NULL)
             trav=trav->next;
         trav->next=node;
     }
 }

 void display(){
     struct Node *trav=HEAD;
     while(trav!=NULL){
         printf(" %d ",trav->data);
         trav=trav->next;
     }
 }

OUTPUT: 10 20 30 40 50 60 70

That’s all with pointer’s for now.

Any suggestions are welcomed to improve the article.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create your website at WordPress.com
Get started
%d bloggers like this: