Multiple choice

struct customer *ptr = malloc( sizeof( struct customer ) ); Given the sample allocation for the pointer ptr found above, which of the following statements is used to reallocate ptr to be an array of 10 elements?

  1. ptr = realloc( ptr, 10 * sizeof( struct customer));

  2. realloc( ptr, 9 * sizeof( struct customer ) );

  3. ptr += malloc( 9 * sizeof( struct customer ) );

  4. ptr = realloc( ptr, 9 * sizeof( struct customer ) );

  5. realloc( ptr, 10 * sizeof( struct customer ) );

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

realloc resizes memory. To expand ptr to 10 elements: ptr = realloc(ptr, 10 * sizeof(struct customer)). Must assign result back to ptr (realloc may move memory). Option D uses 9 elements instead of 10. Options B and E don't assign return value. Option C is syntactically wrong.