I think everyone gets confused by pointers to start with (I have to admit that although I understand what they do, and what you can do with them, I still have my anxieties about implementing them into my code)
Pointers are variables, variables that store a memory address.
A memory address is a location in the computer's memory.
All other variables are stored in the memory, and have an address.
You use a pointer to access the data at the stored memory address.
This is useful because you can manipulate the memory directly, which can be really useful, two reasons why you might use pointers are:
- you want to pass some values in a function, but passing them the normal way (by value) only makes copies of them, so by using pointers, you can manipulate the actual variable stored at the address in the pointer, instead of making copies.
- you're writing a program, but you don't know how much memory you need at the time of compiling (an example might be a map editor program, which is capable of creating maps of different sizes), with pointers you're able to use dynamic memory, i.e. you can access memory that can be allocated during the runtime of the program instead of when the code is compiled.
I don't actually know C, i'm a C++ man *grabs closest shield like object to defendself from C programmers throwing comments about atoi and atof at me
*
(I joke of course), all I know is that you use malloc and free, and when you call malloc you have to use the sizeof keyword to determine the size of the memory you're pointing to (correct me if i'm wrong)
I hope this helped.