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?

2
  • could you add more details? do you get new_ptr=NULL? and what's with the comment ERRORS OUT OF MEMORY? Commented Nov 14, 2013 at 9:52
  • 1
    sizeof myStruct is not your real code, that won't compile. It should be sizeof *new_ptr. Commented Nov 14, 2013 at 9:56

1 Answer 1

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.

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

2 Comments

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!
@MarkLöwe It's due to the operator precedence rules, array subscripting has higher precedence than pointer dereference.

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.