1. ## Understanding pointers

I've read about pointers and watched some tutorials about it, but my understanding about is still shallow. To make up my mind I need to be clear my objective - what are the important terms that I need to know in this topic, and how they relate to each other.

I posted this so that you can probably improve my understanding regarding this topic, give an idea on how to remember or understand it in a new, more preferable way, or correct me if I get the wrong understanding anywhere in topic. Maybe, by just posting this, I can understand the topic even better than just by reading.

Here's the first three terms that I came across:
1. The reference (&) operator.
2. A pointer.
3. The value dereference (*) operator.

So say when I have a variable; int x = 5.
To get to the address of x, which is stored in the memory somewhere, I need to add the prefix & to the variable named x.

So when I typed &x, I can say it is now a variable that holds, the address of x, not the value.

So in conclusion, the address-of operator just 'assign' or give a copy of the memory of variable a to variable b, so that variable b can have access to its memory.

So since we have the the reference of x, &x, that stores its address instead of the value, we can assign it to another variable.

Say, we assign it to variable y. Like this:
Code:
```int y = &x; // this stores the address of x into y, making y a pointer
// that points to x.```
Since we store the address of x into y, we can say that y is a pointer, because y points to the variable whose reference it stores to, in this case, x.

My understanding about 'dereference' (*) operator.

y just store the address of x, but not the value. To get the value of x, one way is to simply use x. Another way is by using dereferencing operator, which is by just adding * before y, we can get the value of x, pointed by y:

Code:
```int *y; //gives value of x since y stores a reference to x, or since x

Is my understanding above right? Is that how they really relate to each other? Please correct the part where my conclusion is wrong and improve the idea of the terms above anywhere possible. Thanks.

2. Originally Posted by Meerul264
1. The reference (&) operator.
I suggest that you call that the "address-of" operator, so as to avoid confusion with C++ references, which is declared with syntax that uses the same symbol.

Originally Posted by Meerul264
So in conclusion, the address-of operator just 'assign' or give a copy of the memory of variable a to variable b, so that variable b can have access to its memory.
Instead of saying "a copy of the memory", "a copy of the address" would be more accurate.

Originally Posted by Meerul264
Code:
`int y = &x;`
The above is wrong because you forgot to declare y to be a pointer, i.e.,
Code:
`int* y = &x;`

3. I see.. thanks for clearing that up.

4. Alright. Um, there are a few important distinctions to be made. int&, int* -- these things are not operators, they are part of a type. The star notation is so you know variable is a pointer type. The & notation is so you know the variable is a reference type. To properly declare a pointer or reference you have to include the proper notation.

The dereferenece operator is usually the star you see in code that uses the pointer.

You might need to adjust the underlying value as in
Code:
`*p += 1;`
or you might be doing something like this:
Code:
```void my_unsafe_strcat(char *s, char *t) {
while ( *s != '\0' ) ++s;
while ( (*s++ = *t++) != '\0' ) ;
}```
All of these are uses of the dereference operator.

I feel you have a good enough grasp on how pointers are made, and that pointers store addresses, it was just these things that needed clearing up.

5. Originally Posted by Meerul264
Here's the first three terms that I came across:
1. The reference (&) operator.
Please be careful not to confuse the reference operator with the address-of operator. You've talked about the address-of operator here - but the & sign is also used to indicate a C++ reference. E.g. int& x = y; x is then an alias of y so can be used to modify y. There's some FAQ stuff here: References, C++ FAQ.

So say when I have a variable; int x = 5.
To get to the address of x, which is stored in the memory somewhere, I need to add the prefix & to the variable named x.

So when I typed &x, I can say it is now a variable that holds, the address of x, not the value.
Yes, &x is a different type to x, and &x holds the address not the value.

So since we have the the reference of x, &x, that stores its address instead of the value, we can assign it to another variable.
The address isn't *stored* by &x, it's just represented by it. Assume that all values of variables are stored in memory (this isn't universally true, but is true for any variable that has its address taken). Memory addresses are, on a 32-bit system, just 32-bit numbers. Maybe the value of x is stored at 0x12340. Then the value of &x is 0x12340.

Say, we assign it to variable y. Like this:
Code:
```int y = &x; // this stores the address of x into y, making y a pointer
// that points to x.```
Since we store the address of x into y, we can say that y is a pointer, because y points to the variable whose reference it stores to, in this case, x.
That's not correct. Because addresses are ultimately just numbers, we can assign them to ints, or interpret them as characters -- whatever we want. "Pointer" refers to a variables type, not its value. In this case y holds the same value that a pointer to x would, but it's not a pointer.

Code:
`int *a = (int*) 0x123044;`
Here 'a' is a pointer to an int. It doesn't matter that we don't know if 0x123044 is a valid address of any variable, or even if it's accessible, but 'a' is still a pointer

.
My understanding about 'dereference' (*) operator.

Code:
```int *y; //gives value of x since y stores a reference to x, or since x
That's not quite right. It's like this:

Code:
```int *y = &x; // this * means y is a pointer type
int a;
a = *y;  // this * is the dereference operator.```
The variable y contains the address of x. It contains it because we assigned it to it earlier. Variables that are stored in memory will be referenced by their addresses in assembly language. The compiler generates code to access these addresses. The variable y might be stored in memory too, in which case the compiler will access memory to get the value of y which is the address of address of x, then load from that address and store into a. It's called indirect addressing -- which is why the derference operator is sometimes called the indirection operator.

Is my understanding above right? Is that how they really relate to each other? Please correct the part where my conclusion is wrong and improve the idea of the terms above anywhere possible. Thanks.
Mostly right I think. I didn't really "get it" until I started working on embedded stuff -- suddenly addresses weren't a mysterious beast in the shadows anymore - they're right there. Then when you go back to C and pointers, though the syntax is still obtuse, the concepts make a lot more sense.