So there has been a lot of talk about these ‘pointers’. My last few C and Objective C questions – and therefore discussions, have gotten a few ‘you must know about pointers!’ reactions. And I always listen to the advisory board (the awesome devs I chat with on and off line).
I know what a pointer is. I think I do. Let’s put my knowledge to the test because I really want to get this right. Here is my best attempt at explaining what a pointer is – rather simplified and I trust you guys and girls to correct me if I am wrong.
A pointer is a variable, and by variable we mean something that doesn’t have a fixed value- it is changeable. The value it holds is the memory address of another variable.
It points to an address – or?
Well, actually it can point to anything, and a reference is actually a pointer (but it points to a variable or object- a sort of alternative identifier)- but often the term pointer is most commonly used to refer to pointers that point to a memory address rather than the high level abstraction we see in for example C# references.
There are different types of memory addresses, physical and logical, but since your app most likely uses a logical (think virtual) address let’s focus on that. When you spin up your application RAM (Random Access Memory) will take hold the memory needed for your application. Variable that you use will get a piece of memory allocated, and that bit of memory will have a memory address. An array of 3 ints will take up 4 blocks of memory (each block is a byte), the last bit indicating the end of the array. You define a pointer like so: int *pointer;
A little spoiler here, but I will talk about pointers in C# tomorrow 😀