473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A question about Pointer And Reference

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
Nov 16 '05 #1
5 1079
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
Nov 16 '05 #2
"lallous" <la*****@lgwm.org> дÈëÓʼþ
news:uX****************@TK2MSFTNGP12.phx.gbl...
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. So *ipa = 47 or *ipa = 0x065FE00?
What's wrong? I feel puzzled.

Nothing's wrong.

Regards,
Elias

Nov 16 '05 #3
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
Nov 16 '05 #4
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
-----Original Message-----
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
.

Nov 16 '05 #5
Perfect, thank u very much!
"Wil" <ma*******@pop.net> дÈëÓʼþ
news:0c****************************@phx.gbl...
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
-----Original Message-----
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
.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
34
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK"...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
11
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.