Multiple choice

Which of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0?

  1. int *ptr = (int *) malloc(10, sizeof(int));

  2. int *ptr = (int *) calloc(10, sizeof(int));

  3. int *ptr = (int *) malloc(10*sizeof(int));

  4. int *ptr = (int *) alloc(10*sizeof(int));

  5. int *ptr = (int *) calloc(10*sizeof(int));

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

malloc(10*sizeof(int)) allocates space for 10 integers. calloc allocates and initializes to zero but takes two arguments (count, size), not one. Option C is the correct malloc syntax. Option B has wrong calloc syntax, and Option E has calloc with single argument which is incorrect.