Start Allocated Counters at Zero
Start the attendance counters at zero
calloc takes an element count and the size of one element, then allocates space with all its bytes set to zero. For the int counters here, each starting value is zero. You still need to handle a NULL result and call free when finished. The call order is calloc(number_of_elements, size_of_one_element).
Unlike malloc, you pass two arguments rather than multiplying them yourself: desks is the count, and sizeof *visits is the size of one counter. The pointer still does not store the count for you.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t desks = 2;
int *visits = calloc(desks, sizeof *visits);
if (visits == NULL) {
return 1;
}
visits[1] = 3;
printf("Visits: %d, %d\n", visits[0], visits[1]);
free(visits);
return 0;
}
Only visits[1] is changed, so the print shows Visits: 0, 3. Had you used malloc, you would need to initialize visits[0] before reading it.
Zeroing bytes gives the integer counters here the value zero. Do not generalize that to every C type: all-zero bytes are not guaranteed to represent a null pointer or floating-point zero. calloc does not remove the need for a failure check, valid indexes, or cleanup.