A pointer is an address into the memory space of your computer. Because all data exists in the memory of the computer, for every variable, there exists exactly one address.
what is useful about pointers?
as a simple example, suppose that you create a very large structure. instead of passing the huge structure between functions, it is much simpler to pass only the address of the function and makes your program leaner and meaner.
pointers are also very powerful-- by having the address where something is located, you can view or even change the contents of that address (dereferencing).
pointers also allow you to get around the limitation in c that you can only return one variable.
in c, items passed into a function get copied in the function so that any changes you may do to them inside the function go away once the function returns. if you wanted to modify some of these values permanently, you could make your function accept pointers to them instead. These pointers would be temporary, but if you modified the contents at the address they pointed to, that change would be permanent.
seansfunction(int *a, int b)
printf("a=%d, b=%d", a, b);
if you executed this code, a would be 12, but b would still be 6
/* a simple explanation of malloc():
malloc() is a request to the operating system for a continuous chunk of memory. The operating system allocates this memory (if possible), and returns a pointer to the start location. because malloc has to be general purpose, it returns a void* and it requires a size (in bytes) that it must allocate.
free() is the counterpart to malloc, and it uses some hidden information to determine how much memory needs to be freed for a given pointer. once a piece of memory is freed, it can be used again.
if you don't free memory when you are done with it, you get what is called a memory leak. What happens is that eventually as your functions are called, you allocate more and more memory until eventually you use up all the available memory and your program cannot continue. at that point, your program will crash.
In C, there are these magical things called void pointers (void *). void * is a pointer to some chunk of memory of unknown type and size. It is very versatile, and maybe a little dangerous. You cannot dereference a void * without casting it first. for example, if I have an array of void *, I could put pointers to ints, structs, other arrays, etc. into it. Later, if I kept track, I could then do something like ((struct structure*)array)->member to get at a member of the structure that the third void* in my array was pointing to.
On a related note, a pointer can transparently be treated as an array. for example: void** data can be treated like data where data is now an array of void*.
You will need to write struct before using the fifo data type-- such is life. i.e. fifo *abigfifo is no good, you need to write struct fifo* abigfifo)
malloc() and free() are your friends. Be sure to use sizeof() in all your calls to malloc and to cast the returned void * appropriately. Calling free() on a pointer that does not point to an allocated chunk of memory will make your program seg-fault (very bad). If you malloc data, be sure to free it when you are done using it.
In C, we like to use libraries for functions that can be used in a bunch of different programs, or even in a single large program to separate functionality. This is a header file which lets you know what the library does and what arguments it takes. The actual code will go into another file called fifo.c . fifo.c MUST include fifo.h . The combination of your fifo.c file and this fifo.h file constitutes a library that can be used again and again. You may even want to save this code for later use :)
Any helper functions or global variables that you want to add beyond those listed in this .h file should be made static-- this will protect those functions and variables from the potentially dangerous actions of end users.