Reputation: 3513
If value are stored in an address, then what does this declaration do
int a = 10;
It store the value in a or in address of &a. And if it store the value in address of a, then why we can't using indirection to this variable like this:
printf("%d", *a);
If not, then how we can say that the each value has an unique address and we can access them using indirection operator.
Edit: If I think that indirection is used only on pointer, then consider this:
int b[10];
b[0] = 4; // Give it some value
Now we know that b[0] is a scalar quantity and can be used anywhere where scalar value are required. But in this case, we can use indirection to it like this:
printf("%d", *b); // print 4
Isn't interesting to see that we can use pointer on this scalar variable, but cannot use on variable declare without array.
In my opinion, compiler automatically generates an indirection for variable declare like this:
int a = 4;
So, indirection is not possible on this because we are putting another indirection on it which is not legal except in cases when variables are declares like that:
int a = 4;
int *b = &a;
int **c = &b;
Edit 2: You can take scanf("%d", &a)
as a proof which says store the value in address of a not in a.
Upvotes: 2
Views: 565
Reputation: 123578
First of all, you cannot use the indirection operator on anything that does not have a pointer type.
Remember that declarations in C are based on the types of expressions, not objects; the declaration
int a = 10;
says that the expression a
has type int
, and will evaluate to the value of 10
(at least until someone assigns a different value to it). So when you write
printf("%d\n", a);
the compiler knows to retrieve the value stored at the memory location bound to the expression a
. Think of this as a direct access.
Now consider the declaration
int *p = &a;
This declaration says that the expression *p
has type int
, and will evaluate to the value of whatever p
is currently pointing to (in this case, *p
will evaluate to 10
since p
is initialized with the address of a
); the indirection operator is part of the declaration (and is bound to the declarator *p
, not the type specifier int
). The variable p
is a pointer to an integer, and it stores the address of another integer variable (in this case, the address of the variable a
). Thus, if we want to access the integer value, we must dereference p
:
printf("%d\n", *p);
This is an indirect access to the value 10
; instead of accessing it through the variable a
, we're accessing it through p
which points to a
.
Upvotes: 0
Reputation: 754920
When the variable is of type int
(rather than int *
), the compiler knows that it needs to do the indirection for you, and you shouldn't try to make it do it. That is, when you have int a = 10;
, the compiler stores the value at a memory location which is represented by &a
(ignoring registers) and it knows that when you write printf("%d\n", a);
it is required to fetch the value stored at &a
automatically without you having to think about telling it to dereference something.
When the variable is of type int *
(e.g. int *p
), there are two ways you can read the value:
p
p
These are two different operations, so two notations are needed:
int a = 10;
int *p = &a;
int *q = p;
int r = *p;
Of course, p
also has its own address.
Upvotes: 2
Reputation: 5239
When you do
int a = 10;
The compiler allocates memory of enough size to accomodate an int
. This location has to be identified(this is at this particular place i.e. the address) and labelled(this location is called a
).It then stores the data at that point. The label allows you easier access. If the language was designed in a way that you would only have access to locations via pointers (i.e dereferencing them to get values) it will be difficult.
You can say its like, You live on some piece of land which can be pinpointed by an exact latitude and longitude. But its better to keep a name for that location, MyHouse etc. rather than referring to the latitude/longitude everytime. Both name and longi/lati refer to the same thing.
a
is the label. &a
is more like longi/lati
Edit: Regarding int b[10]
. array names are not plain labels. They also act as pointers and hence you can use *
to dereference them
Upvotes: 0
Reputation: 26144
It's not really clear what the question is here, so I'll just explain the situation...
Each (global) variable is located somewhere in memory. When it is assigned a value, that value will in memory at the location of the variable.
If you, in C, use the variable, you actually use the value stored at the location of the variable.
One way to see this is that if &
takes the address of an object, and *
dereferences (follows) a pointer, then * &a
is the same as simply a
.
Upvotes: 0
Reputation: 91139
Up to a certain point you are right: a
stores an int
and lives at the address &a
.
But indirection can only be used on pointers. So you could do either of
int a = 10;
int *p = &a;
printf("%d", a);
printf("%d", *(&a));
printf("%d", *p);
Upvotes: 3