I never grasped very well the concept of pointers and their usefulness... maybe that's why i didn't progressed very much with programming.
@Shiroyasha @hideki Why is it that people has so much trouble with pointers? It’s not that hard, really. But maybe nobody explains it right.
Think about variables as containers that can hold a specific data value in the memory. The memory address that references this memory address is a Pointer. So, Pointers hold memory addresses, while variables hold the actual data.
Pointers work the same way as variables, except they contain a numeric value that represents a memory location. This means that you can assign a value to them (a memory address), or operate with its value, which is useful if you want to access multiple locations if you are, for instance, dealing with an array of data. Pointers types are specified with a * symbol in the type of the variable, indicating that it is a pointer and not a regular variable.
In C, you can increase or decrease the address value by using simple arithmetical operations. For instance, you can have a pointed named “valPtr”, and increase it’s value by 1. But you have to take into consideration the size of the value being held in said address. For instance, if “valPtr” is a pointer to a char (char valPtr), doing valPtr++ will increase the memory address by 1. But if it was an short (short \valPtr) it would be increased by 2, not 1. This is because sizeof(short) is 2, therefore valPtr++ would increase the address by 2.
Last thing you need to know is that you can directly reference the value being held in the memory location specified in the pointer by using a pointer deference (*). So if I have a pointer “short *valPtr”, I can retrieve it’s value like: “short val = *valPtr”. And I can also assign a value like: “*valPtr = 65534”.
Pointers are just memory addresses so regular variables are also referenced by pointers. If you need to reference the memory location of a specific variable, use the reference symbol (&). For instance, having valPtr as a short* and val as a short (regular variable), you can do the following: “valPtr = &val”. This will make “valPtr” hold the memory location of the variable “val”. Or in other words, valPtr now points to val. So “val” and “*valPtr” are the same.
But if you do this, always have in mind the scope of your variable. Variables declared (not necessarily defined) inside of a function are always placed in the stack of the function. Therefore these addresses will no longer reference a variable once the IP is outside of the function declaring it. If you need to hold a value outside of a function then you need to use memory allocation.
Another thing to note is that you can have pointers of pointers. For instance, if you need to have multiple strings, which are arrays of characters, then you can do so declaring a “char**” pointer.
Also, same as variables, data structures, subroutines and functions can also be referenced by pointers. And yes, you can call a function by it’s pointer in C as well. But that would make this post even longer and more complex than it already is so, I’ll leave that to your investigation.