by melvinroest · 6y ago · 0 comments
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
- 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 (look for the regs option)
[0] 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-d...
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"And this is how many times we're allowed to use the dereference operator: one time
I can see what you meant, but I initially thought this was trying to say that a pointer declared with a single asterisk could only be dereferenced once and never dereferenced again afterwards (as if there was a "one-time-use only" property to pointers). In the same vein, the "super_pointer" example could be misinterpreted like "Oh, so if I want a pointer that will let me dereference it on 5 different occasions, then I'll need to declare it with 5 asterisks?" I think a better way would be to replace the "how many times we're allowed to use the dereference operator" explanation and start with explaining the idea of a double pointer
int **doublePtr
in depth first. With a solid understanding of a double pointer (an address that points to a memory location that contains an address that points to another memory location), making the mental jump to understand int *****super_pointer
should be easier.Also, I think low addresses aren't used for various reasons, but I believe the kernel is stored in the top, high-address region of the address space. Otherwise, I think this is a really solid explanation of pointers!
Ah, haha, I see! You're right. This could've been me back in the day.
> Also, I think low addresses aren't used for various reasons, but I believe the kernel is stored in the top, high-address region of the address space.
Ah, yea, I was foggy on this.
For what reasons aren't they used then?
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.
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.
Then you come across
extern int *(*f)();
int *g = f(0), h = *(******f)(*g);When I look at a statement like this, I really have to sit for it and just reason though it a few times.
You might be able to tell: I'm not a C programmer ;-)
I simply self-taught C in order to get some homework assignments done with some security classes. Example: we had to make our own packet sniffer in C. Actually, we had to make all kinds of tools in C.