0

I'm having an issue with pointers. I've read through 30+ posts on this subject and none match my setup. Here's what I'm trying to do:

void doSomething(myStruct **myList)
{
    resizeMyList(myList,5);

    myList[0] = '42';
    myList[1] = '43'; // ERRORS OUT OF MEMORY
}

void resizeMyList(myStruct **theArray,int size)
{
    myStruct *new_ptr = realloc(*theArray,(size * sizeof myStruct));

    if(new_ptr)
         *theArray = new_ptr;
    else
         printf("died");
}

After my resize function executes myList fails to get the new pointer. What am I doing wrong?

Mark Löwe
  • 572
  • 1
  • 10
  • 24
  • could you add more details? do you get new_ptr=NULL? and what's with the comment ERRORS OUT OF MEMORY? – H_squared Nov 14 '13 at 09:52
  • 1
    `sizeof myStruct` is not your real code, that won't compile. It should be `sizeof *new_ptr`. – unwind Nov 14 '13 at 09:56

1 Answers1

3

You do

myList[0] = ...

but myList is a double pointer so it should be

(*myList)[0] = ...

Also, you try to assign multi-character literals to a structure.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Wow, parens?? I was actually doing the *myList, and forgot to put it in my sample code. After adding it, it worked. Amazing how the littlest things make all the difference. Thank you! – Mark Löwe Nov 14 '13 at 09:56
  • 1
    @MarkLöwe It's due to the [operator precedence rules](http://en.cppreference.com/w/c/language/operator_precedence), array subscripting has higher precedence than pointer dereference. – Some programmer dude Nov 14 '13 at 09:58