ProgrammingHard

Which function call allocates storage for an array of 100 doubles, all set to 0.0, in one step?

  1. A. double *d = malloc(100);
  2. B. double *d = calloc(100, sizeof *d);
  3. C. double *d = malloc(100 * sizeof(int));
  4. D. double *d = realloc(100, sizeof *d);

Correct answer: B. double *d = calloc(100, sizeof *d);

calloc with 100 elements of sizeof *d zero-fills the bytes. On IEEE systems the all-bits-zero pattern is 0.0 for double, so the values read as zero.

More C Programming questions

Exams that ask this topic