11 ms·
Here's my explanation. A pointer stores a memory address. But I hear you say: can't an int or long also do that? All you need is the number of the memory addr
by melvinroest 6y ago
Here's my explanation.
A pointer stores a memory address.
But I hear you say: can't an int or long also do that? All you need is the number of the memory address.
Well, the magical thing about a pointer is that it can use a special operator!
What? (I hear you say)
Yes!
Here is what it does: it allows you to obtain the value that is stored at that memory address.
What does a pointer store again? (a memory address)
But it can also obtain a value at that memory address.
You can't do that with an int, I've tried [0].
We call this operator the dereference operator [1].
It looks like a star
*
That's the one. That's the dereference operator.
One of the confusing things is that when you declare a pointer, you also use a *
Example
int *a;
//or
int * a;
//or
int* a;
In those 3 examples, the * is not a dereference operator. It simply indicates that it is a pointer. In this case we have an int pointer.
I always like to view it as: we have an int! And this is how many times we're allowed to use the dereference operator: one time
How many times are you allowed to use it on this pointer?
int *****super_pointer
That's right, five times!
So when you have
int val = 10;
int *a = val;
Then what is the value of a? It's 10. What do pointers store? Addresses. So what address is this? Address 10!
But when it comes to memory addresses in computers, there is a tricky part to it. In normal operating systems, low addresses are reserved for special things, I think it's the kernel in this case. So if you dereference that particular address, the OS/runtime environment won't allow you to. They will give you an error called a segmentation fault.
So you need to get a valid address of the variable val, but how do you do that? We have another operator for that, which I like to call the address-of operator.
So when you have
int val = 10;
int *a = &val; // & is the address-of operator
Then the variable a has the valid memory address we want!
There's much more to learn, but I'll leave it at this for now.
---
I recommend people who start learning pointers/programming to use GDB with the TUI graphical interface. One could also print the values, but it's easier to see what really happens. For that there are two steps you need to do:
- Watch a GDB with TUI tutorial: https://www.youtube.com/watch?v=AxsSXMPD3N4 https://www.youtube.com/watch?v=AxsSXMPD3N4
- And then also display all the cpu registers. The reason for that: your variable values will be visible in there (in hexadecimal).
All the GDB commands: https://sourceware.org/gdb/onlinedocs/gdb/TUI-Commands.html https://sourceware.org/gdb/onlinedocs/gdb/TUI-Commands.html (look for the regs option)
[0] https://stackoverflow.com/questions/35379191/cannot-cast-variable-type-to-its-pointer-type-e-g-int-to-int-i-guess-its-a https://stackoverflow.com/questions/35379191/cannot-cast-var...
[1] Side note: I used to have trouble with the word dereferencing. If you have to, here is why it's called that way: https://stackoverflow.com/questions/35372786/what-does-the-de-prefix-in-dereference-mean-is-there-a-linguistic-explanation https://stackoverflow.com/questions/35372786/what-does-the-d...
- bluetomcat 6y ago> In those 3 examples, the * is not a dereference operator, it simply indicates that it is a pointer This is one of the confusing parts for beginners, but in actuality, C declarations are meant to mirror the use of the variable. In that sense, you can think of the asterisk in the declaration as the same unary operator: int *a; // dereferencing "a" gives you type "int"
- oldsklgdfth 6y agoThis is a really good explanation. You understand what's going on with pointers.
- FakeRemore 6y agoThis is similar to the idea that a number isn't always a number. A number can be categorical, in which case comparison operators don't make any sense. They can be a label or a name, in which case addition or other mathematical operators don't make any sense. The same way a number can be an address, which has its own sets of operations that operate on the idea of an address. As an example, statistics has the idea of ordinal values vs interval values, where they have different properties that make them not comparable to each other and shouldn't be mistakenly be confused when working on them.
- melvinroest 6y agoHaha, I do view mathematics as "software engineering but then purely for numbers". I view things like the quadratic formula as little software programs. I view complex numbers as the necessary "software system" to allow for negative square roots. Other views I have on mathematics look at this perspective and shout "blasphemy! How could you insult such an intellectual pursuit!". But it works quite well for in me when I'm doing math. I like your lesson. I never saw it that clearly. And with respect to numbers, I don't think I saw this at all.
- mk_chan 6y agoI would recommend working with https://godbolt.org https://godbolt.org instead of gdb. It abstracts away the unnecessary stuff and highlights lines corresponding to asm from source and vice versa quite conveniently