C Language / Pointers

It is a variable which is used to store the address of another variable.

Pointer Syntax
Example Details
int *ptr; pointer to an integer
float *ptr; float
double *ptr; double
char *ptr; char

Pointers Operators
Pointers Operators Pointers Operators Symbols Used to
Address Operator & Address of Operator with %u or %p formats
indirection Operator * Value at Address

Sample Program on pointers. Output

#include <stdio.h>
void main ()
{
int a = 20; //variable declaration
int *ptr; // pointer variable declaration
ptr = &a;
// Variable Address stored in pointer variable
printf("variable Address a: %p\n", &a );
// address stored in pointer variable
printf("Address stored in ptr variable: %p\n", ptr );
// access the value using the pointer
printf("Value of var variable: %d\n", *ptr );
}

variable Address a: 0x7ffd0f850424
Address stored in ptr variable: 0x7ffd0f850424
Value of var variable: 20
Program on Pointers as arguments to functions Output

#include<stdio.h>
int addnos(int *a, int *b);
void main()

{
int a=10, b=10;
printf("a value = %d\n",a);
printf("b value = %d\n",b);
printf("sum = %d",addnos(&a,&b));
}
int addnos(int *a, int *b)
{
return (*a + *b);
}

a value = 10
b value = 10
sum = 20

Pointer to pointer
Pointer which points to the address of another pointer is known as pointer to pointer.

Pointer to pointer Syntax
int **pr;

Sample Program for pointer to pointer. Output

#include <stdio.h>
void main()
{
int n=10;
int *ptr1;
//Pointer for num
int **ptr2; //pointer to pointer
ptr2 =&ptr1;
ptr1= &n;
ptr2= &ptr1;
printf("\n n Value : %d", n);
printf("\n ptr1 Value : %d", *ptr1);
printf("\n ptr2 Value : %d", **ptr2);
/*Possible ways to find address of num*/
printf("\n Number Address : %u", &n);
printf("\n ptr1 Address : %u", &ptr1);
printf("\n ptr2 Address : %u", &ptr2);
}

n Value : 10
ptr1 Value : 10
ptr2 Value : 10
Number Address : 1849661948
ptr1 Address : 1849661952
ptr2 Address : 1849661960

Explanation
Variable= value Variable Address
N=10 &n=1849661948
*Ptr1=10 &ptr1=1849661952
**Ptr2=10 &ptr2=1849661960

Arrays and pointers
Array name is used as a base address of the array / first element address and we assign array address into a pointer to do our required operation.

Let us take Array name = a
Int a [5] ={10, 20, 30, 40, 50};

Array a[0] a[1] a[2] a[3] a[4]
Addresses 1005 1007 1009 1011 1013
Elements 10 20 30 40 50


Int occupies 2bytes of memories.
int *ptr; //pointer to an integer
ptr =&a[0]; // ptr = contains address of a[0] base address

Programto find the sum of all elements of an array. Output

#include<stdio.h>
void main()
{
int a[5],*ptr,i,n=5,sum=0;
ptr =a;
printf("Enter 5 Numbers\n");
for(i=0;i<n;i++)
scanf("%d", ptr +i);
for(i=0;i<n;i++)
sum=sum+(*(ptr +i));
printf("\nsum= %d",sum); }

Enter 5 Numbers
1
2
3
4
5
sum=15

Array of pointers
It means we assign array addresses to the pointers.

Sample Program Output

#include <stdio.h>
void main ()
{
int a[] = {1, 2, 3};
int i, *ptr[3];
for ( i = 0; i < 3; i++)
ptr[i] = &a[i];

//assign array address to pointer
for ( i = 0; i < 3; i++)
printf("Array Element Value a[%d] = %d\n", i,
*ptr[i] );
}

Array Element Value a[0] = 1
Array Element Value a[1] = 2
Array Element Value a[2] = 3

Note
S. No Operations on pointers Example
1 Pointer variables can be used for Pointer arithmetic *ptr1 + *ptr2
2 Pointer variables can be used for Pointer comparison If(*ptr1 + *ptr2) Statement1


Home     Back