Pointer/variable references.

Heya again!

This time, i've got a very simple question - perhaps the answer i more complicated though, hehe.

So i've got some degree of pointer-understanding gathered in my brain...

1
2
3
int var;
int * mypointer;
mypointer = &var;


Okay, so now mypointer stores the adress of var...

And we can edit the value of var by using:
*mypointer = 5

But one thing i have been wondering about, is setting a varibles adress.

Let's say...

1
2
3
int var1;
int var2;
&var1 = &var2;


Will this make var1's adress to be the same as var2's?
They can't share the adress, so does var2 get relocated to another adress?

Seems to me like this code compiled would make var1 want to move into var2's adress...

So what happens, does var2 have to move to another adress? Will the compiler tell var1 that he isn't allowed to move into var2's adress?

Edit: Oh, another question aswell... Am just reading through "Pointers and arrays" tutorial here, and when you want to give the adress of an array to a pointer, you don't specifically have to add the reference operator?

1
2
3
int * mypointer;
int myarray[5];
mypointer = myarray;


Is this equal to this?

1
2
3
int * mypointer;
int myarray[5];
mypointer = &myarray;


Or is the second case maybe even illegal to use? (though, it would certainly confuse me if it was)

Anyway, are those two code snippets equal?
Last edited on

So what happens, does var2 have to move to another adress? Will the compiler tell var1 that he isn't allowed to move into var2's adress?
Neither. That code is illegal. EDIT2: actually, I guess the latter XD. I guess I should've read that closer.

&var is what's called an "rvalue". Basically, rvalues can't be reassigned.

Other rvalues include string literals and constants.

So it's kind of similar to doing this:

5 = 10; // wtf?
The compiler simply won't let you do it.

Same thing with &a = &b;. It'll give you an error because &a can't be reassigned.

EDIT:

Anyway, are those two code snippets equal?


No.

The thing to note here is that array names without their brackets are not pointers. They're array names. The confusing bit is that array names can be implicitly cast to pointers.

The other confusing part is that arrays are different types from individual elements in the array. For example, an array of 5 integers is a different type than a single integer.

So the difference is this:
arrayname without the brakets can be cast to a pointer to a single element in the array. Specifically, the first element in the array. Basically arrayname is shorthand for &arrayname[0].

On the other hand.... &arrayname gets you a pointer to the entire array (not to an individual element). This is the same physical address in memory, but the type is different.

It's easier to illustrate with typedefs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int int5[5];

// now, 'int5' is an array of 5 ints.  So these lines are equivilent:
int5 a;
int b[5];

// here, we want to have a pointer to a single integer
int* ptr1 = a;  // this works because 'a' can be cast to a pointer to the first element
ptr1 = &a[0];  // this also works because we're getting the pointer to the first element

// but if we have a pointer to the whole array...
int5* ptr2 = &a;  // you need the & here because we need an int5 pointer, not an int pointer

// ptr2 would be dereferenced like so:
(*ptr2)[3] = 5;  // same as a[3] = 5; 
Last edited on
Aha.. The illustration throw me off a bit - But otherwise, fantastic explanation!

It's quite clear now!

I usually have some syntax related questions... I have always been a grammar-bitch ;)

And while we're at it...

Defining a pointer, there is no difference from initializing here, right?

1
2
3
int* mypointer;
int * mypointer;
int *mypointer;


They basically all define a pointer in the same way?
Last edited on
I've had that asterisk everywhere. To the left, to the middle, to the right, multiple and variable spaces for each, and I even threw a ****ing newline between the asterisk and int keyword, and it's always the same thing (EDIT: Please don't ask why I had a newline in such an inappropriate place).

Don't you just love C and C++ for their whitespace-mostly-ignoring abilities?

-Albatross
Last edited on
Aww, fantastic!

Good to see somebody else dedicated to syntax ;)
Topic archived. No new replies allowed.