A Simple C Program To Understand How Pointers Work

Leave a Comment
/* Its a simple C program, declared with an integer variable with a default value, & then the reference of which is passed to a pointer. The reference of that variable is also passed to a pointer to pointer. Then we have tried with the various ways of printing it, lets see how it works. The output is displayed below.*/
//CODE:-
#include<stdio.h>
#include<conio.h>
int main( )
{
int a = 10;
//Variable with Default Value
int *pa; //Pointer
int **ppa; //Pointer to pointer
pa = &a; //Assigning reference of variable to the pointer
ppa = &pa; //Assigning reference of pointer to pointer of pointer
printf("Address of a = %p\n", &a); //Simply printing the address of variable 'a'
printf("Value of pa = Address of a = %p\n", pa); //printing the value of pointer
printf("Value of *pa = Value of a = %d\n", *pa); //printing the value at location stored in 'a'
printf("Address of pa = %p\n", &pa); //Printing the address of pointer
printf("Value of ppa = Address of pa = %p\n", ppa); //Printing the address of 'pa'
printf("Value of *ppa = Value of pa = %p\n", *ppa); //Printing the value at location in 'ppa'
printf("Value of **ppa = Value of a = %d\n", **ppa); //printing value of 'a' by double reference
printf("Address of ppa = %p\n", &ppa); //Printing address of 'ppa'
getch();
return 0;
}



Screenshot:
A Simple C Program To Understand How Pointers Work
A Simple C Program To Understand How Pointers Work

0 comments: