Pointers in C
Pointers in C
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
var2=&var1;
Direct Access: You can directly use the variable name to access the variable.
Example:
#include<stdio.h>
int main()
ptr=&a;
printf(“Direct Access, a=%d\n”,a);
return 0;
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:
#include<stdio.h>
int main()
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;
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.
Example:
#include<stdio.h>
#include<stdlib.h>
int main()
int a=5;
ptr=&a;
free(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 *ptr;
ptr = arr;
for (int i = 0; i < 3; i++)
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()
ptr = &arr[2];
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:
#include <stdio.h>
int main() {
int *ptr;
ptr = &arr[0];
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 *ptr;
ptr = &arr[4];
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.
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 ()
var = 10;
ptr1 = &var;
ptr2 = &ptr1;
return 0;
}
An Array of Pointer:
Syntax:
Example:
#include <stdio.h>
int main ()
int i, *ptr[3];
ptr[i] = &a[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>
{
printf("Before adding value inside function num=%d\n",num);
num=num+100;
int main()
int x=100;
change(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>
(*num) += 100;
int main()
int x=100;
return 0;
}
The Advantages of Pointers:
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
Pointers can cause several errors, such as segmentation errors or unrequired memory access