Accessing the value of an integer through pointer.

A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the “address of a variable”. The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.


display integer variable value using pointer in c


A pointer is declared as follows:

int *ptr;

where *ptr is a pointer of int type.



A Program to display the value of an int variable through pointer is listed below

#include
void main()
{
int k;
int *ptr;
clrscr();
k=10;
ptr=&k;
printf("\n Value of k is %d\n\n",k);
printf("%d is stored at addr %u\n",k,&k);
printf("%d is stored at addr %u\n",*ptr,ptr);
*ptr=25;
printf("\n Now k = %d\n",k);
getch();
}

Output of the program is:
Value of k is 10
10 is stored at addr 65524
10 is stored at addr 65524
Now k=25.