C Array Size

📏 C Array Size (Beginner → Advanced)
Knowing the size of an array in C language is very important to avoid out-of-bounds errors, write generic code, and solve DSA & interview problems.
1️⃣ What Does “Array Size” Mean?
Array size can mean:
Total number of elements
Total memory occupied
Size of one element
2️⃣ Finding Array Size Using sizeof() ⭐ (Most Important)
Example
✔ Works only inside the same scope where array is declared
3️⃣ Why sizeof(arr) Works?
Because:
arris a real arraysizeof(arr)returns total memory of array
Example (on 4-byte int):
4️⃣ Array Size in Function ❌ (Common Mistake)
📌 Inside a function, array becomes a pointer
📌 sizeof(arr) returns pointer size (4 or 8 bytes)
5️⃣ Correct Way: Pass Size as Parameter ⭐
✔ Best practice
6️⃣ Array Size for Different Data Types
| Type | Example | Size per element* |
|---|---|---|
| int | int a[5] | 4 bytes |
| float | float f[5] | 4 bytes |
| double | double d[5] | 8 bytes |
| char | char c[5] | 1 byte |
*May vary by system
7️⃣ Size of Multidimensional Arrays ⭐
Number of Rows & Columns
8️⃣ Dynamic Array Size (Using malloc)
📌 Size must be stored separately
📌 sizeof(arr) gives pointer size ❌
9️⃣ String Size vs Length ⭐
✔ Very common interview question
🔟 Common Mistakes ❌
❌ Using sizeof on array inside function
❌ Confusing bytes with number of elements
❌ Hardcoding array size
❌ Accessing beyond array length
📌 Interview Questions (Must Prepare)
How to find array length in C?
Why
sizeof(arr)fails inside function?Difference between
sizeofandstrlen?Size of 2D array calculation?
How to handle size of dynamic arrays?
🔥 Real-Life Importance
Prevents buffer overflow
Safer loops
Generic functions
DSA correctness
Embedded & system programming
✅ Summary
✔ Use sizeof(arr)/sizeof(arr[0]) in same scope
✔ Pass size to functions
✔ Inside functions, array becomes pointer
✔ sizeof returns bytes, not element count
✔ Critical for interviews & real projects
