Item:
What if I tell you to write me a program that will support a set of counters, but the number of counters will be entered by the user when the program starts. We encode this using an array of integers allocated on the heap.
int *counters = malloc(numOfCounters * sizeof(int));
Malloc works with memory directly, so by its nature it returns a pointer. All Objective-C objects are allocated using the malloc heap, so they are always pointers.
Item:
What if I told you to write me a function that read the file and then run another function when it was done. However, this other function was unknown and will be added by other people, people whom I did not even know.
For this, we have a “callback”. You should write a function that looks like this:
int ReadAndCallBack(FILE *fileToRead, int numBytes, int whence, void(*callback)(char *));
This last argument is a function pointer. When someone calls the function you wrote, they do something like this:
void MyDataFunction(char *dataToProcess); ReadAndCallBack(myFile, 1024, 0, MyDataFunction);
Item:
Passing a pointer as an argument to a function is the most common way to return multiple values from a function. In Carbon OSX libraries, almost all library functions return an error status, which creates a problem if the library function should return something useful to the programmer. This way you pass the address where you want the function to pass information to you ...
int size = 0; int error = GetFileSize(afilePath,&size);
If the function call returns an error, it is in error , if there was no error , error will probably be zero, and size will contain what we need.