A question about Pointer And Reference

L

lallous

Hi,
int a = 47;
int* ipa = &a;
Then ipa contains the address of a. Correct.

But I think, if &a=0x065FE00, then the second sentence is:
int* ipa = 0x065FE00;
so *ipa should contains the address of a?
Yes true. But the address of 'a' can change everytime you modify the program
therefore you should not really put hardcoded address values, that's why you
should use the '&' operator.
What's wrong? I feel puzzled.
Nothing's wrong.

Regards,
Elias
 
T

Tommy Shore

Hi,
I saw a sentence :
int a = 47;
int* ipa = &a;
Then ipa contains the address of a.

But I think, if &a=0065FE00, then the second sentence is:
int* ipa = 0065FE00;
so *ipa should contains the address of a?

What's wrong? I feel puzzled.

Thanks.

Tommy Shore
 
T

Tommy Shore

lallous said:
Hi,

Yes true. But the address of 'a' can change everytime you modify the program
therefore you should not really put hardcoded address values, that's why you
should use the '&' operator.
So *ipa = 47 or *ipa = 0x065FE00?
 
L

lallous

Hi,

Look, when you say:
int *ipa = 0x12345
and do: printf("%x\n", ipa) you will see 12345
if you do printf("%d\n", *ipa) then you get the integer value pointer by
'ipa' = what integer is at address [0x12345] (in your case 47)
int* ipa = 0065FE00;
so *ipa should contains the address of a?
No, *ipa == value of 'a'


Sorry about my last post.

Regards,
Elias
 
W

Wil

I think you are confused by the syntax of pointer
initialization in C, which **is** confusing in that it
overloads the dereference operator "*". Specifically, in
the initialization statement

int * ipa = &a; // initialization of the pointer;
// "&a" is the address of an integer

the "*" means you are declaring "ipa" to be a pointer to
int, and you initializing it by making it point to "a".
Now once you have declared the pointer, however, the
meaning of "*" is different in an assignment statement:

*ipa = b; // assignment to "* ipa" (i.e., to "a");
// b is the value of an integer

Here "*" means to dereference the pointer (i.e., go
to "a") and store the integer "b" there. So the right-
hand-side of the initialization requires an address, but
the right-hand-side of the assignment statement requires a
value, even though the "*" operator appears on the left-
hand side of both statements. To make this a little
clearer, consider that once you have declared "ipa" to be
a pointer, then you can make it point to a different
integer by storing a new address in "ipa":

ipa = &c; // assignment to "ipa" (not to "* ipa");
// "ipa" now points to "c", not to "a"

Here there is no "*" operator, even though there was one
in the intialization statement, which made "ipa" point
to "a" originally. So the "problem" is just one of C
syntax, in which "*" means different things in different
places. Did I understand you correctly?

Wil
 
T

Tommy Shore

Perfect, thank u very much!
Wil said:
I think you are confused by the syntax of pointer
initialization in C, which **is** confusing in that it
overloads the dereference operator "*". Specifically, in
the initialization statement

int * ipa = &a; // initialization of the pointer;
// "&a" is the address of an integer

the "*" means you are declaring "ipa" to be a pointer to
int, and you initializing it by making it point to "a".
Now once you have declared the pointer, however, the
meaning of "*" is different in an assignment statement:

*ipa = b; // assignment to "* ipa" (i.e., to "a");
// b is the value of an integer

Here "*" means to dereference the pointer (i.e., go
to "a") and store the integer "b" there. So the right-
hand-side of the initialization requires an address, but
the right-hand-side of the assignment statement requires a
value, even though the "*" operator appears on the left-
hand side of both statements. To make this a little
clearer, consider that once you have declared "ipa" to be
a pointer, then you can make it point to a different
integer by storing a new address in "ipa":

ipa = &c; // assignment to "ipa" (not to "* ipa");
// "ipa" now points to "c", not to "a"

Here there is no "*" operator, even though there was one
in the intialization statement, which made "ipa" point
to "a" originally. So the "problem" is just one of C
syntax, in which "*" means different things in different
places. Did I understand you correctly?

Wil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top