Pointers in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Pointers in C?

A pointer is a variable pointing to the address of another variable. It is declared along with an
asterisk symbol (*). The syntax to declare a pointer is as follows:

datatype *var1

The syntax to assign the address of a variable to a pointer is:

datatype var1, *var2;

var2=&var1;

In How Many Ways Can You Access Variables?

You can access a variable in two ways.

 Direct Access: You can directly use the variable name to access the variable.

 Indirect Access: You use a pointer to access that variable.

Example:

#include<stdio.h>

int main()

int a=5, *ptr;

ptr=&a;
printf(“Direct Access, a=%d\n”,a);

printf(“Indirect Access, a=%d\n”,*ptr);

return 0;

What Are the Different Types of Pointers?

There are majorly four types of pointers, they are:

 Null Pointer

 Void Pointer

 Wild Pointer

 Dangling Pointer

Null Pointer:

If you assign a NULL value to a pointer during its declaration, it is called Null Pointer.

Syntax:

Int *var = NULL;


Example:

#include<stdio.h>

int main()

int *var = NULL;

printf(“var=%d”,*var);

Void Pointer:

When a pointer is declared with a void keyword, then it is called a void pointer. To print the value of
this pointer, you need to typecast it.

Syntax:

void *var;

Example:

#include<stdio.h>

int main()

int a=2;
void *ptr;

ptr= &a;

printf("After Typecasting, a = %d", *(int *)ptr);

return 0;

Wild Pointer:

A wild pointer is only declared but not assigned an address of any variable. They are very tricky,
and they may cause segmentation errors.

Example:

#include<stdio.h>

int main()

int *ptr;

printf(“ptr=%d”,*ptr);

return 0;

}
Dangling Pointer

 Suppose there is a pointer p pointing at a variable at memory 1004. If you deallocate this
memory, then this p is called a dangling pointer.

 You can deallocate a memory using a free() function.

Example:

#include<stdio.h>

#include<stdlib.h>

int main()

int *ptr=(int *)malloc(sizeof(int));

int a=5;

ptr=&a;

free(ptr);

//now this ptr is known as dangling pointer.

printf(“After deallocating its memory *ptr=%d”,*ptr);

return 0;

}
What Are the Use Cases of Pointers in C?

 Pointer arithmetic

 Pointer to pointer

 Array of pointers

 Call by value

 Call by reference

Pointer arithmetic:

 Increment: You can use this operator to jump from one index to the next index in an array.

Syntax:

ptr++;

Example:

#include <stdio.h>

int main() {

int arr[3] = {50, 150, 200};

int *ptr;

ptr = arr;
for (int i = 0; i < 3; i++)

printf(“Value of *ptr = %d\n”,*ptr);

printf(“Address of *ptr = %d\n”,ptr);

ptr++;

 Decrement: You can use this operator to jump from one index to the previous index in an array.

Syntax:

Ptr--;

Example:

#include<stdio.h>

int main()

int arr[3]={50, 150, 200};


int *ptr;

ptr = &arr[2];

for (int i=0;i<3;i++)

printf("Value of *ptr = %d\n", *ptr);

printf("Address of *ptr = %d\n\n", ptr);

ptr--;

 Integers added to a Pointer: You can use this operator to jump from one index to the next ith
index in an array.

Syntax:

ptr+=i; // where ‘i’ is an integer


Example:

#include <stdio.h>

int main() {

int arr[5] = {10, 100, 200, 300, 500};

int *ptr;

ptr = &arr[0];

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

printf("Value of *ptr = %d\n", *ptr);

printf("Address of *ptr = %d\n\n", ptr);

ptr=ptr+2;

 Integers Subtracted from a Pointer: You can use this operator to jump from one index to the
previous ith index in an array.

Syntax:
ptr-=i; // where ‘i’ is an integer

Example:

#include <stdio.h>

int main() {

int arr[5] = {10, 100, 200, 300, 500};

int *ptr;

ptr = &arr[4];

for (int i = 0; i<5; i++)

printf("Value of *ptr = %d\n", *ptr);

printf("address of *ptr = %d\n\n", ptr);

ptr-=2;

}
 Precedence:

 Operators * and & are given the same priorities as unary operators (increment++, decrement--).

 The unary operators *, &, ++, - are evaluated from right to left in the same expression.

 If a P points to an X variable, then you can interchange X with *P.

Expression Equivalent Expression

Y=X+1 Y=*P+1

X=X+10 *P=*P+10

X+=2 *P+=2

++X ++*P

X++ (*P)++
Pointer to Pointer:

In this situation, a pointer will indirectly point to a variable via another pointer.

Syntax:

Int **ptr;

Example:

#include <stdio.h>

int main ()

int var, *ptr1, **ptr2;

var = 10;

ptr1 = &var;

ptr2 = &ptr1;

printf("Value of var = %d\n", var );

printf("Value available at *ptr1 = %d\n", *ptr1 );

printf("Value available at **ptr2 = %d\n", **ptr2);

return 0;

}
An Array of Pointer:

An array of the pointer is an array whose every element is a pointer.

Syntax:

int *arr[n] //where n is size of array.

Example:

#include <stdio.h>

int main ()

int a[3] = {10, 100, 200},n=3;

int i, *ptr[3];

for ( i = 0; i < 3; i++)

ptr[i] = &a[i];
}

for ( i = 0; i < n; i++)

printf("Value of a[%d] = %d\n", i, *ptr[i] );

return 0;

Call By Value:

In ‘call by value’, you must copy the variable's values and pass them in the function call as a
parameter. If you modify these parameters, then it doesn't change the value of the actual variable.

Example:

#include<stdio.h>

void change(int num)

{
printf("Before adding value inside function num=%d\n",num);

num=num+100;

printf("After adding value inside function num=%d\n",num);

int main()

int x=100;

printf("Before function call x=%d \n",x);

change(x);

printf("After function call x=%d \n",x);

return 0;

Call By Reference:

In call by reference, you must take the variable's address and pass it in the function call as a
parameter. If you modify these parameters, then it will change the value of the actual variable as
well.
Example:

#include<stdio.h>

void change(int *num)

printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n",*num);

int main()

int x=100;

printf("Before function call x=%d \n",x);

change(&x);//passing reference in function

printf("After function call x=%d \n",x);

return 0;

}
The Advantages of Pointers:

 Pointers in C programming are helpful to access a memory location

 Pointers are an effective way to access the array structure elements

 Pointers are used for the allocation of dynamic memory and the distribution

 Pointers are used to build complicated data structures like a linked list, graph, tree, etc

The Disadvantages of Pointers:

 Pointers are a bit difficult to understand

 Pointers can cause several errors, such as segmentation errors or unrequired memory access

 If a pointer has an incorrect value, it may corrupt the memory

 Pointers may also cause memory leakage

 The pointers are relatively slower than the variables

You might also like