📖 Scenario: You are writing a simple C program to store the ages of 5 people. You want to allocate memory dynamically for an array of 5 integers and initialize all values to zero.
🎯 Goal: Learn how to use the calloc function to allocate and initialize memory for an integer array, then print the values.
📋 What You'll Learn
Create a pointer variable to hold the address of the allocated memory.
Use calloc to allocate memory for 5 integers.
Check if the memory allocation was successful.
Print the values stored in the allocated memory to confirm they are zero.
Free the allocated memory at the end.
💡 Why This Matters
🌍 Real World
Dynamic memory allocation is used in programs where the amount of data is not known before running, such as reading user input or handling large datasets.
💼 Career
Understanding <code>calloc</code> and memory management is essential for C programmers working in systems programming, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Declare a pointer for integer array
Declare a pointer variable called ages of type int * to hold the address of the allocated memory.
C
Hint
Use int *ages; to declare a pointer to integers.
2
Allocate memory using calloc
Use calloc to allocate memory for 5 integers and assign it to ages. Include #include <stdlib.h> at the top if not already present.
C
Hint
Use calloc(5, sizeof(int)) to allocate memory for 5 integers initialized to zero.
3
Check allocation and print values
Write an if statement to check if ages is not NULL. Then use a for loop with variable i from 0 to 4 to print each value in ages[i].
C
Hint
Use if (ages != NULL) to check allocation. Use a for loop to print each element.
4
Free the allocated memory
Add a line to free the memory pointed to by ages using the free function after printing the values.
C
Hint
Use free(ages); to release the memory after use.
Practice
(1/5)
1. What does the calloc function do in C?
easy
A. Copies memory from one location to another
B. Allocates memory but leaves it uninitialized
C. Frees previously allocated memory
D. Allocates memory for multiple elements and initializes all bytes to zero
Solution
Step 1: Understand calloc purpose
calloc allocates memory for an array of elements and initializes all bytes to zero.
Step 2: Compare with other functions
malloc allocates memory but does not initialize it, free releases memory, and memcpy copies memory.
Final Answer:
Allocates memory for multiple elements and initializes all bytes to zero -> Option D
Confusing calloc with malloc (uninitialized memory)
Thinking calloc frees memory
Assuming calloc copies data
2. Which of the following is the correct syntax to allocate memory for 10 integers using calloc?
easy
A. int *arr = calloc(10);
B. int *arr = calloc(sizeof(int), 10);
C. int *arr = calloc(10, sizeof(int));
D. int *arr = calloc(10 * sizeof(int));
Solution
Step 1: Recall calloc parameters
calloc takes two arguments: number of elements and size of each element.
Step 2: Match correct order and types
int *arr = calloc(10, sizeof(int)); correctly uses calloc(10, sizeof(int)). Other options have wrong order or missing arguments.
Final Answer:
int *arr = calloc(10, sizeof(int)); -> Option C
Quick Check:
calloc(count, size) = correct syntax [OK]
Hint: First argument = count, second = size in calloc [OK]
Common Mistakes:
Swapping the two arguments
Passing only one argument
Multiplying inside calloc instead of separate arguments
3. What will be the output of this code snippet?
int *ptr = calloc(5, sizeof(int));
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
medium
A. Random garbage values
B. 0 0 0 0 0
C. Compilation error
D. Segmentation fault
Solution
Step 1: Understand calloc initialization
calloc sets all allocated bytes to zero, so all integers start as 0.
Step 2: Analyze the loop output
The loop prints each element, all zero, so output is "0 0 0 0 0 ".
Final Answer:
0 0 0 0 0 -> Option B
Quick Check:
calloc zeroes memory = zeros printed [OK]
Hint: calloc memory is always zeroed, so prints zeros [OK]
Common Mistakes:
Expecting garbage values like malloc
Forgetting to free memory (not causing output error here)
Assuming uninitialized values
4. Identify the error in this code:
int *arr = calloc(5, sizeof(int));
arr[5] = 10;
free(arr);
medium
A. Accessing out-of-bounds index
B. Missing free call
C. Incorrect calloc syntax
D. No error, code is correct
Solution
Step 1: Check array indexing
Array allocated has indices 0 to 4. Accessing arr[5] is out-of-bounds.
Step 2: Confirm other parts
calloc syntax is correct and free is called properly.
Final Answer:
Accessing out-of-bounds index -> Option A
Quick Check:
Index 5 invalid for size 5 array [OK]
Hint: Array indices go from 0 to size-1 [OK]
Common Mistakes:
Using index equal to size (off-by-one error)
Forgetting to free memory
Misunderstanding calloc parameters
5. You want to allocate memory for a 2D array of 3 rows and 4 columns using calloc. Which code snippet correctly does this?
hard
A. int (*arr)[4] = calloc(3, sizeof(*arr));
B. int **arr = calloc(3, sizeof(int*)); arr[0] = calloc(4, sizeof(int));
C. int *arr = calloc(3 * 4, sizeof(int));
D. int arr[3][4] = calloc(3, 4);
Solution
Step 1: Understand 2D array allocation
int (*arr)[4] = calloc(3, sizeof(*arr)); allocates 3 rows of arrays each with 4 ints, using pointer to array syntax.
Step 2: Check other options
int **arr = calloc(3, sizeof(int*)); arr[0] = calloc(4, sizeof(int)); only allocates first row's columns, not all rows. int *arr = calloc(3 * 4, sizeof(int)); allocates flat 1D array, not 2D. int arr[3][4] = calloc(3, 4); is invalid syntax.
Final Answer:
int (*arr)[4] = calloc(3, sizeof(*arr)); -> Option A