What is pointer and what does it do

Definition: Pointer is a variable that stores the address of an object. Here object can be variable.

In C/C++ language, we determine the address using & sign. For example

int n;  //Declaration of n
n = 23; // Initialization of n

Here we can find the address of n using &n.

We declare pointer as

int *ptr;

Now to store address of n, initialize pointer variable as

ptr = &n;

Now the value of pointer ptr will be address of variable n. So we can find the address of variable using pointer and & sign.

 We can initialize pointer at the time of declaration like

int *ptr = &n;

See the following example

#include<stdio.h>
int main()
{
 int n=23;
 int *ptr;
 ptr= &n;
 printf("Address of %d = %u\n",n,&n);
 printf("Address of %d = %u",n,ptr);
 return 0;
}

Here pointer variable ptr will print the address of variable n. Address of variable n is the value of pointer ptr like 23 is the value of n. Address of pointer ptr will be different.

We can find the address of pointer ptr using &ptr. See the following example

#include<stdio.h>
int main()
{
 int n=23;
 int *ptr;
 ptr= &n;
 printf("Address of %d = %u\n",n,&n);
 printf("Address of pointer = %u",&ptr);
 return 0;
}









Popular posts from this blog