简体   繁体   中英

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.

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.

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);

you did the referencing and derefrencing of a pointer at the same tim

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM