If you are reading this you want to know more about c pointers. Even if you don’t program in C very often, understanding pointers gives you a deeper understanding how programming and memory works “under the hood”. Then we will finish up with different types of pointers you will encounter. That’s true but it’s also just scratching the surface.
Learning pointers will make you a better programmer. When a variable gets declared, memory to hold a variable of that type is allocated at an unused memory location.
For , tells the compiler how much memory to store starting at that address. The more detailed answer has to do with call semantics. When you call a function in C, the value of any parameters are literally copied into the function’s call stack. Pass a char and 1-byte is copied into the function.
A C compiler converts C source code to assembly source code. What happens when you need to pass a 100k element int array into a function?
When an array is created, the array variable cannot be reassigned.
Here is an example operator exists to make it easier. You can have a pointer to a pointer, and a pointer to a pointer to a pointer and so on down the rabbit hole.
The , what actually happens is the compiler allocates memory for the entire array and then assigns a pointer to the array variable, in this case myarray, holding the address of the first element in the array.
Some people get confused and start thinking you can interchange pointers and arrays. You can assign an array variable to a pointer of the same type but not the opposite.
A pointer holds a reference to a variable; the reference being the memory address stored in the pointer.
When we access the value at that reference, we de-reference the pointer.