The routine below fails on the last line. It ran fine on an 8 bit machine some years ago and the lines were actually in a for statement. The last line bombs it when I compile it as a standard unix tool with Xcode. Any help about why it fails much appreciated. Need to get the original routine running again.
Been 20+ years since I maintained the code. I can't find where any of the pointers were initialized. Guess I wonder why it ran on the 8 bit computers (used Aztec C) and won't run now.
I know I'm ignorant, but give me an example of initializing one of the pointers, please.
When you create a pointer, it allocates just enough memory to handle the address to a variable. It doesn't actually allocate space for the variable it's meant to hold.
1 2 3
int * pExample;
int bob;
*example = bob; //this is not valid because space has not been attached to the pointer pExample.
If we were to try and access *example, it probably wouldn't give anything (??? because it's NULL ???).
There are many solutions, one concerning stack and the other concerning heap.
1 2 3 4
int *pExample;
int bob;
pExample = &bob; //This assigns pExample the address of bob. All variables have they're own unique adress. Now we can do...
*pExample = 3; //This sets the space assigned to pExample to 3. Now bob = 3.
This is one method of allocating and assigning a pointer in stack. This isn't normally done since we have limited space on stack and this can be costly.
The more common method of doing this is to allocate space on heap like so:
1 2 3
int *pExample; //Allocate space on stack for pointer to hold memory in heap.
pExample = newint; //This allocates space on the heap. So now we can do something like...
*pExample = 3; //This assigns the memory we allocated on heap to 3.
This covers the basics. If you want more information on the difference on stack and heap, please consult the reference on the site.