I have a structure E.g.
typedef struct{
char charArray[10];
int someInteger;
}[B]myStruct[/B];
Then inside main() I do this
void main(void)
{
myStruct a[1];
myStruct *sPtr = a;
func(&sPtr);
...
// code that continues to work with the struct array (a)
}
The above passes a pointer to the pointer that points to the start of my structure array. The reason I'm not passing the pointer by value is because I may have to change the value of the pointer within func, and will need this change available after func returns back in main.
void func(myStruct **ptr)
{
// this function reads in from a binary file using fread. Each read reads in sizeof(myStruct) bytes from the file.
// Everytime I successfully read in that many bytes from the file, I then want to re-allocate the amount of memory available for my structure array (a) in main().
// So on the first successful read, I'll first malloc enough space to store the first record, and on subsequent calls I'll realloc enough space to store existing records plus one more.
// Here's my attempt at a re-alloc
ptr = (myStruct **) realloc (*ptr, (loopCounter+1) * sizeof(myStruct));
// realloc above does not return NULL (i.e. failure),
but when I try to store something in:
*ptr[loopCounter+1] = structure that holds contents of read
I get a memory error
}
Truth is *ptr[0] on the first iteration before the realloc, holds stuff fine and I can access it, change it, etc. But after I realloc memory available to the array, I still can't write to array indexes greater than zero.
What am I doing wrong?