Home > Software design >  I thought that i can print value of variable by using its actual address . but i can't instead
I thought that i can print value of variable by using its actual address . but i can't instead

Time:10-20

int num,address ;
address = &num ;  
num = 2029 ;
printf("\n%d",address) ;

It is printing the address of num but address of num is being printed.

Is it possible to print value of variable by accessing its address not by name like we do in the scanf() function?

CodePudding user response:

To output a variable value using the variable address you should write for example

int num = 2029;
int *address = #
printf( "\n%d",*address) ;

That is you need a pointer that will store the address of the variable and to get access to the variable itself you need to dereference the pointer.

Or if you want to use integer types then you can do the following

#include <stdio.h>
#include <stdint.h>

int main( void ) 
{
    int num = 2029;
    uintptr_t address = ( uintptr_t )( void * )&num;

    printf( "num = %d\n", *( int * )( void * )address );
}

From the C Standard (7.20.1.4 Integer types capable of holding object pointers)

The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

uintptr_t

The same is valid for the type intptr_t.

CodePudding user response:

You need to use a pointer:

int num, *address; // declares address as a *pointer* to int
address = &num;    // assigns the address of num to address variable
num = 2029;        
printf( "%d\n", *address ); // we *dereference* the address variable
                            // to access the contents of num

A pointer is any expression whose value is the location of an object or function in a running program's execution environment (i.e., an address). A pointer variable can be used to store the address of another object or function.

A simple pointer variable is declared as

T *ptr; // for any type T

The type of the variable ptr is "pointer to T", or T *, which is specified by the combination of the type specification T and the declarator *ptr. The "pointer-ness" of the variable is specified by the presence of the * operator in the declarator.

Because whitespace isn't significant in this case, you can declare that pointer as any of1

T *ptr;
T* ptr;
T*ptr;
T      *         ptr;

but it will always be parsed as

T (*ptr);

In the code above we declare address as a pointer to int:

int num, *address;

The "int-ness" of address is provided by the type specifier int and the "pointer-ness" of address is provided by the declarator *address.

The unary & operator is used to obtain a pointer value - in the statement

address = &num;

the expression &num yields a pointer to the variable num. The type of the expression &num is int * (pointer to int). The type of the variable address is also int *.

 address == &num // int * == int *

To access the value stored in num from address, we dereference address using the unary * operator:

printf( "%d\n", *address );

The expression *address has type int, and acts as a kinda-sorta alias for the variable num:

*address == num // int == int

Pointer types and declarations can get arbitrarily complex:

T *aptr[N];     // aptr is an array of pointers to T
T (*ptra)[N];   // ptra is a pointer to an array of T
T *fptr();      // fptr is a function returning a pointer to T
T (*ptrf)();    // ptrf is a pointer to a function returning T

T *(*(*p[N])())[M] // p is an array of pointers to functions returning
                   // pointers to M-element arrays of pointers to T

In both declarations and expressions, the unary * operator has lower precedence than the postfix [] and () operators, so *a[i] will be parsed as *(a[i]) and *f() will be parsed as *(f()) (we're dereferencing the results of a[i] and f()).


  1. C programmers tend to use the convention T* ptr;, because they want to emphasize that ptr has pointer type. However, it is always interpreted by the compiler as T (*ptr);.

    I will spare you my usual rant on the practice.
  • Related