Another way to learn pointers is via assembly. Either learn assembly or just look at the disassembled code. Look under the hood and it'll become clearer.
Of course the best way is to write your own C compiler... but that may be overkill for most.
I did not find that all that surprising, but the first time I saw that a[i] can also be written as i[a], I was very irritated.
Well, it is a little bit incomplete I think. A pointer contains both the address of a variable and a type. That is why pointer arithmetic works. Without type of pointer, p++ is not calculatable.
The type is implicitly stored as the pointers type. Casting a pointer can change a piece of memory to be interpreted as any type you want. Although the cast might not make logical sense or may even caused undefined behavior in C.
Pointer arithmetic works off of whatever the current type is, as the compiler needs to know how to the memory offsets.
Given the way the C-standard is relatively carefully written, that explanation is a useful lie, but not necessarily true in every implementation of C.
But it is possible to imagine completely exotic CPU architectures, where there is a separate type of register that can only be used for pointers and it is impossible to convert to int; you could still write C compilers for that architecture and be 100% conforming to the standard.
Most processors a pointer just gets stuffed into a register and then that's used by a assembly instruction to read/write a memory location. But it's certainly possible to have systems where it's not so simple.
"Hey!"
(smacks on head)
"It's an int that points to a variable."
"Now get off the computer and take out the trash without talking about garbage collection!"
"but it could be an int32_t and sometimes..."
(smack)
In other words, the statement is incorrect.
'void' is a type, but in C values of the type void are not first class. Ie a function can return a value of type void, but you can't assign it to a variable.
Doesn't it technically also contain its own address? So it contains the address of another variable and the address of itself. Pointers are hard...
You can "get" the address of a variable (or more generally, of any l-value) with the & operator. That operator is nothing else but an "escape" character that says "Dear C, I don't want you to load the current value of that variable. Give me just the address where it lives so I can load it later".
(Same applies to storing instead of loading)
Huh? Then you probably don't understand the meaning of "contains" here. Or what variables are in general. Variables are hard...
All our bytes of ram are numbered. We read a value from byte number 8 and that value is 17.
"Byte number 8" is a pain to remember or calculate so we give it a label and let the computer keep track. That's a "variable".
What does that 17 mean though? Is it a value we want to use later on? It might be. Or it could be referring to Byte 17 in our ram. Byte 17 happens to have the value 42 so if we use Byte 8 as a pointer that's the value we end up with.
But it's entirely our choice. It's just a number and we can treat it as a value or a pointer to another memory address as we like.
The subject of pointers was generally believed to be scary among fellow students and many of them bought pretty fat books that were dedicated solely to the topic of pointers.
However, when I reached Chapter 5 of the The C Programming Language (K&R), I found that it did a wonderful job at teaching pointers in merely 31 pages. The chapter opens with this sentence:
> A pointer is a variable that contains the address of a variable.
The exact point at which the whole topic of pointers became crystal clear was when I encountered this sentence in § 5.3 Pointers and Arrays:
> Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as * (a+i).
Indeed, it was easy to confirm that by compiling and running the following program:
The output is: C was the first serious programming language I was learning back then and at that time, I don't think I could have come across a better book than K&R to learn this subject. Like many others, I too feel that this book is a model for technical writing. I wish more technical books were written like this with clear presentation and concise treatment.