Vinit Shah
Vinit Shah

Reputation: 1

How *(&i) dereferencing works in c without pointer pointing?

#include<stdio.h>

void main()
{
    int a=10;
    printf("%d\n",a);
    printf("%d\n",*(&a));
}

As variable 'a' is of type integer and not a variable pointer pointing to itself so how dereferencing is working here. I maybe wrong in understanding.

Upvotes: 0

Views: 65

Answers (3)

Coder Manish
Coder Manish

Reputation: 41

You did the referencing and dereferencing of a pointer at the same time.

By putting asterisk you are dereferencing pointer that is asking for value at the address and inside the () you are referencing the variable that is you are replacing the &a with the address of the variable after & so the address is replaced with the * the value at the address is fetched. So it works.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

For the property of the unary & and * operator:

The unary & operator yields the address of its operand

and

The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type "pointer to type", the result has type "type".

In your case

*(&a)

is the same as

* (pointer to object 'a') or, * (address of variable 'a')

which is the same as

 a

So, this

 printf("%d\n",*(&a));

is similar to

printf("%d\n",a);

Upvotes: 1

dbush
dbush

Reputation: 223972

The unary & operator takes the address of its operand. So the result of &a is a pointer of type int * which can subsequently be dereferenced via the unary * operator.

Upvotes: 1

Related Questions