Multiple choice

void *ptr; myStruct myArray[10]; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"?

  1. ptr = ptr + sizeof(myStruct);

  2. ++(int*)ptr;

  3. ptr = ptr + sizeof(myArray);

  4. increment(ptr);

  5. ptr = ptr + sizeof(ptr);

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

When working with void pointers, you must manually increment by the size of the data type. Option A ptr = ptr + sizeof(myStruct) correctly advances the pointer by one struct size. Options B, C, and E are incorrect syntax or logic, and option D is not a standard function.