Complete the code to allocate memory for 10 integers using calloc.
int *arr = (int *)[1](10, sizeof(int));
The calloc function allocates memory for an array of elements and initializes all bytes to zero.
Complete the code to check if calloc failed to allocate memory.
if (ptr == [1]) { printf("Allocation failed\n"); }
When calloc fails, it returns NULL. Checking for NULL ensures the program handles allocation failure.
Fix the error in the calloc usage to allocate memory for 5 doubles.
double *data = (double *)calloc([1], sizeof(double));The first argument to calloc is the number of elements, here 5. The second argument is the size of each element, which should be sizeof(double), but the blank is for the number of elements.
Fill both blanks to allocate memory for 8 characters and check if allocation succeeded.
char *buffer = (char *)[1]([2], sizeof(char)); if (buffer == NULL) { printf("Memory allocation failed\n"); }
Use calloc to allocate and zero-initialize 8 characters. Then check if the pointer is NULL to detect failure.
Fill all three blanks to allocate memory for 12 floats, check allocation, and free the memory.
float *arr = (float *)[1]([2], sizeof(float)); if (arr == [3]) { printf("Allocation failed\n"); } else { free(arr); }
Use calloc to allocate 12 floats. Check if the pointer is NULL to detect failure. If allocation succeeds, free the memory.