ProgrammingHard
Which function call allocates storage for an array of 100 doubles, all set to 0.0, in one step?
- A. double *d = malloc(100);
- B. double *d = calloc(100, sizeof *d);
- C. double *d = malloc(100 * sizeof(int));
- 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
- A function gets a pointer to a linked list and must keep the list unchanged but needs to walk it twice. Which kind of pointer parameter is…
- A program keeps 10^5 small objects in a free list of pointers. Which operation is O(1) per object?
- int arr = malloc(5 sizeof arr); for (int i = 0; i < 5; i++) arr[i] = i i; int q = arr + 2; printf("%d", q[1]); What is printed?
- struct N { int v; struct N next; } a = NULL; for (int i = 1; i <= 3; i++) { struct N n = malloc(sizeof n); n->v = i; n->next = a; a = n; }…
- Two pointers p and q point into the same block allocated with malloc(10 sizeof(int)). Which subtraction is defined?
- A linked list of length n is reversed in place with a loop. How much extra memory is used?