A question about Pointer And Reference

  • Thread starter Thread starter lallous
  • Start date Start date
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
 
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
 
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?
 
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
 
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
 
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
 
Back
Top