Hello Code Explorers! 👋
Welcome to Part 3 of our Pointers Series in C.
So far, we have explored basic pointers and arrays
+ pointers.
Now, it’s time to tackle a limitation of arrays in C and learn about dynamic
arrays. 🚀
🔹 Problem with Static Arrays
- A static
array has a fixed size determined at compile-time.
- Once
declared, its size cannot change.
Example:
#include <stdio.h>
int main() { int arr[5] = {1, 2, 3, 4, 5};
// trying to add more elements is not allowed // arr[5] = 6; // ❌ Error: Out of bounds
printf("Array elements: "); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); }
return 0;}
🧾 Output:
Array elements: 1 2 3 4 5
Problems with static arrays:
- Fixed
size → waste memory if not fully used
- Not
enough size → cannot store more elements
- Cannot
grow or shrink at runtime
🔹 Enter Dynamic Arrays
A dynamic array is an array whose size can be
decided at runtime.
It is created using pointers and memory allocation, instead of fixing
the size in advance.
Benefits:
- Allocate
memory only when needed
- Can resize
the array if required
- More
flexible than static arrays
🔹 Memory Allocation: malloc and calloc
Imagine you want a special container to store items:
- malloc
→ gives you the container but doesn’t clean it; it may have junk inside
- calloc
→ gives you the container and clears it, all items start at 0
✅ malloc Example
#include <stdio.h>#include <stdlib.h> // required for malloc
int main() { int n = 5; int *arr;
// create memory to store 5 integers arr = (int *)malloc(n * sizeof(int));
printf("Enter 5 numbers:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); }
printf("Array elements: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
free(arr); // release memory return 0;}
- malloc
gives us a block of memory large enough to store 5 integers
- sizeof(int)
tells us how big an integer is in bytes
- int
*arr tells the computer: “store the address of this block of memory here”
✅ Typecasting (int *) tells the
computer: “This block of memory will hold integers.”
Think of it as labeling the container so the computer knows what type of
items it will hold.
✅ calloc Example
#include <stdio.h>#include <stdlib.h>
int main() { int n = 5; int *arr;
// create memory to store 5 integers, all initialized to 0 arr = (int *)calloc(n, sizeof(int));
printf("Array elements after calloc: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
free(arr); return 0;}
🧾 Expected Output:
Array elements after calloc: 0 0 0 0 0
Explanation:
- calloc
creates the memory block and sets all values to zero
- We
still typecast (int *) to label the container for integers
🔹 Key Differences: malloc
vs calloc
|
Feature |
malloc |
calloc |
|
Memory content |
Garbage (uninitialized) |
Initialized to zero |
|
Parameters |
1 (total bytes) |
2 (num elements, size each) |
|
Use case |
When you will initialize manually |
When you want memory zeroed automatically |
Why two ways?
- malloc
→ faster if you plan to fill all memory yourself
- calloc
→ safer when you want a “clean container” to start with
🔹 Key Points to Remember
✅ Static arrays have fixed size;
dynamic arrays can grow/shrink
✅
Use pointers to store addresses of dynamically allocated arrays
✅
malloc → uninitialized memory, calloc → zero-initialized memory
✅
Always use free() to avoid memory leaks
✅
Typecasting tells the computer what type of data your memory will
hold
🧠Mini Assignments
🎯 Assignment 1:
Create a static array of size 5. Try to add a 6th element and see what happens.🎯 Assignment 2:
Use malloc to create a dynamic array of size n (input by user). Input values and print the array.🎯 Assignment 3:
Use calloc to create a dynamic array of size n. Print the elements before assigning values.🎯 Assignment 4:
Compare malloc and calloc by printing uninitialized memory vs zero-initialized memory.💬 Share your results with
the ProgVeda Community! 🌟
Understanding dynamic arrays and typecasting is a major step toward
writing flexible, efficient C programs.
Post a Comment