About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Access Address of Variable using Pointer

#include<stdio.h>
void main( )
{
int i = 3 ;
printf("\nAddress of i = %u", &i ) ;
printf("\nValue of i = %d", i ) ;
printf("\nValue of i = %d", *( &i ) ) ;
}

Output:

Address of i = 65524
Value of i = 3
Value of i = 3

Explanation :

Variable ‘i’ is declared of type integer in the program , now suppose the integer variable can have value 3 stored inside it and it has address 65524.
Value of the Variable at Particular Memory location Pointer
Suppose we want to access the address of the variable i then we can use ampersand operator to find the address of the variable.
&i
and whenever we use ampersand and value at operator together then we will get actual value of the variable.
* (&i) = Value at (&i)
       = Value at (65535)
       = 3

Values of Variable and Address

Variable ExpressionMeaning of the VariableValue
&i
Address of i65524
iValue of variable i3
*( &i )
Value of variable i3

No comments: