A very quick question on declaring stuff

If int* x defines a pointer, then does int& y define a reference?

I know it sounds like a silly question, but it's screwing me up at this one part
yea you got it, *x is a pointer and &y is a reference.

so x would "point" to the memory location of whatever its equal to.

lets say
1
2
int bill = 15;
int* x = bill;


the memory address of bill will be some address location and thats what x will be equal to, the memory address of bill.
ex. bill is stored at location 100A, x is equal to 100A.

if u use the & operator, you "dereference" the memory address, basically meaning you take whats actually at that memory location.

so int y = &x will give you 15.

try it out in a simple program and here's some more info about it if you still don't get it.

http://www.cplusplus.com/doc/tutorial/pointers/
closed account (zb0S216C)
Yes, it does. Note that references must have an initializer.

tech junkie wrote:
int* x = bill; (sic)

Where's your address-of operator?

tech junkie wrote:
if u use the & operator, you "dereference" the memory address (sic)

Only pointers can be dereferenced. The address-of operator reveals the address of the first byte of the operand given to it; nothing more, nothing less. When the address-of operator is used in combination with pointers, the address-of operator is used to give the pointer an address to point to.

tech junkie wrote:
so int y = &x will give you 15. (sic)

No. In C++, a memory address and a int are two different things. Only pointers can refer to an address in memory.

Wazzak
Last edited on
Technically, you are declaring though, not defining.
closed account (zb0S216C)
I guess define and declare can be used interchangeably when referring to variable, no?

Wazzak
Not really sure,(i'm still a nub after a couple years of c++(really :( )) but i thought that declaring a variable was saying something like "int x;" whereas defining it would be "x = 3"

Please don't take me word for word all the time. I like to imagine I have approximate knowledge of many things.
To make you feel better, you are correct. If you ever forget, look at the processor statement define:

 
#define WM_CUSTOM 10001 


Here you are defining that WM_CUSTOM means 10001 all throughout your code. There is no declaration here because it isn't a variable where as

 
int x;


is declared but not defined. WR417H is correct in his assumption. Now defined and initialized are the same thing though.
Topic archived. No new replies allowed.