1

Hi I am slowly getting my head around pointers and indirection but am still having a little trouble.

In my main function i am creating an array of structs (computers) using malloc

int arraySize = 0;//This may be the first issue
Computer    *ptrComputer = NULL; 
ptrComputer = (Computer*) malloc(sizeof(Computer) * arraySize);

I am then passing the pointer to a function that reads data out of a file and into the array

arraySize = readFileToArray(&ptrComputer, arraySize);

int readFileToArray(Computer **compArray, int arraySize){

Computer newComp;
int foundARecord = 0;
/*File stuff*/

   arraySize = extendArrays(compArray, arraySize, no_elements);

    /*Use fscanf to read file data into the newComputer variable*/
    printf("g %i\n", arraySize);
    *compArray[arraySize - 1] = newComp;//set the newly created part of the array to newComputer


return arraySize;
}

int extendArrays(Computer **compArray, int arraySize){
   arraySize++;
   //Resize the computer array
   *compArray = (Computer*)realloc(*compArray, (sizeof(Computer)*(arraySize + 1)));
   return arraySize;
}

Now as far as i understand, i am passing the address of ptrComputer to the readFileToArray() function. It is then passing that same address to the extendArrays() function which resizes it. I am then trying to assign newComp to the location in memory that compArray points to. This works as long as i try to write to index 0 but any others cause xcode to throw an exc_bad_access error. This is all very confusing, can anyone with a bit of experience with this see where i am going wrong? It was working before when i was passing the actual pointer to the readFile function but it only worked once (think i was reallocating a copy of the pointer) Any help would be very muchly appreciated Thanks

4
  • 1
    The first thing you should have done was check the return value of malloc(). Commented May 25, 2011 at 1:47
  • Also, you shouldn't immediately assign the return value of realloc to *compArray, as it may return NULL, in which case the former value of *compArray is still valid. Commented May 25, 2011 at 1:53
  • I do have checking implemented on both, just didnt include them to keep the size of the code down Commented May 25, 2011 at 1:59
  • Do not cast the return value of malloc. Casting the return value of malloc serves no purpose and may hide errors the compiler would catch otherwise. Commented May 25, 2011 at 8:24

1 Answer 1

2

Instead of this:

    *compArray[arraySize - 1] = newComp;//set the newly created part of the array to newComputer

Try this:

    (*compArray)[arraySize - 1] = newComp;//set the newly created part of the array to newComputer

This way you explicitly dereference compArray before subscripting.

Sign up to request clarification or add additional context in comments.

3 Comments

That appears to have done it, thank you so much. Could you please explain the difference between the two?
The array subscripting operator has a higher precedence than the dereferencing operator. So *x[i] evaluates as *(x[i]), not (*x)[i].
So the way i was doing was trying to deference compArray[arraySize - 1] instead of just compArray

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.