Reputation: 55
#include<stdio.h>
main() {
int a,*p;
a=2;
p = &a;
printf("The address %p contains the address of a=2:%p\n",&p,p);
printf("The address of %d is %p\n",a,&a);
return 0;
}
The result is:
The address 0028FF18
contains the address ofa=2: 0028FF1C
is this correct? if not, what's the correct statement??
The address of 2 is 0028FF1C
is this correct? if not, what's the correct statement?? */
Upvotes: 2
Views: 135
Reputation: 89
Look at the diagram below:
+---------+
| |
| 2 | Value of a=2
| |
+---------+
Address=0028FF1C
+---------+
| |
|0028FF1C | P stores address of a
| | So p=0028FF1C
+---------+
Address=0028FF18
So now your first question is
1. The address 0028FF18 contains the address of a=2: 0028FF1C. Is this correct?
Answer: Yes, it is correct as 0028FF18 is address of p and it contains &a i.e. address of a which is 0028FF1C.
Now coming to second question
2. The address of 2 is 0028FF1C. Is this correct?
Answer: This is little bit confusing statement (can't say it's perfectly correct) as address of a is 0028FF1C and value of a is 2. So it might be better if you print it like:
printf("The address of a is %p\n",&a);
So it will give output:
The address of a is 0028FF1C.
This statement seems better as here we are printing address of variable not the address of a value which that variable stores.
Upvotes: 1
Reputation: 134336
Maybe a simple analogy will help you understand better.
int a = 5;
int *p = &a;
5
is the value of int
variable a
.&a
is the value of int *
variable p
.*p
is the int
value at the address held by int *
variable p
.Upvotes: 0
Reputation: 13981
In your situation, both are correct!
Just imagine a variable as a box with label(variable name) on it, and the box itself has an address(memory address),such as Room 304 on 3rd floor, for anyone to locate it. In the box lays a note(value) tells you what it is(a int 3
, a char 's'
, a float 3.14
etc).
While a pointer is also a box with the exception that it contains a note with another box's address on it.
int
?How it works on an variable int a=2
?
You just say, hey I want to access a
(the box with label a on it), someone(your compiler along with OS) just find the box and give it to you. You open the box, and see a note reading 2
(might as well as type int
). You can change it with another note you just scribbled, say, 3
.
int *
?Similarly, say we have int *p = a
. We get the box with label p
on it, open it, and it says Room 305 on 5th floor
. So instead of saying 'I want the box a
', this time we say 'bring the box in Room 305 on 5th floor'(which is what *p
means).
*p = 3;
means: bring the box which box p
points to and change its value to 3.int *q = &a
: bring a new box(put it somewhere), and fill it with a note reads the address of a
&p
: give me the address of box p
int **p
(pointer to pointer) and more.Upvotes: 0
Reputation: 2544
A picture is worth a thousand words, here is what p and a like in memory.
left are address, and right are variable names.
+---------------+
0028FF18 | 0028FF1C | p
+---------------+
0028FF1C | 2 | a
+---------------+
Upvotes: 0
Reputation: 1009
A pointer is really just a special kind of number — an address used to locate another piece of data. In your example, p
is the numeric address to a
, and &p
is a pointer to the address stored in p
. &p
is a pointer to a pointer.
Upvotes: 0
Reputation: 1561
Regarding a:
printf("The value of a is %d.", a); // The value of a is 2.
printf("The address of a is %p.", &a); // The address of a is 0028FF1C
Regarding p:
printf("The value of p is the address of a is %p.", p); // The value of p is the address of a is 0028FF1C
printf("The address of p is %p.", &p); // The address of p is 0028FF18
I think of memory on a computer as a street full of addresses (a really long street), and at each address lives a single value. The variables a
and p
are simply syntax needed for us as programmers to conceive of these memory locations. So, a
is a representation for the address 0028FF1C and the current resident of that address is 2 ... in the future, 2 could move out and 4 could move in, but the address would be the same.
Likewise with p
- his address will always be 0028FF18, and it just so happens that the address of a
is the current resident at this location, but later a different value could move in here (e.g., p = &b
) and p
's address would still be 0028FF18.
Upvotes: 1
Reputation: 330
p has its own address which is &p, but the value of p is the address of a.
Upvotes: 1