Understanding Arrays in C: Boundaries, Memory & Hidden Traps!

 

Hey again, brilliant coders! 👩💻👨💻
Welcome to Part 2 of our journey into Arrays in C. You’ve already learned how to declare, initialize, and loop through arrays.
🎯

Now, let’s explore some deep conceptual insights every C programmer should know. These ideas will help you write safe, optimized, and professional-grade C code. 🚀

🧱 (A) Boundary Checking

In C, the compiler does not check whether the index you’re using is valid or not.
That means no built-in boundary checking — you’re the one responsible!
😬

Example: Out of Bounds Access

#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
printf("%d\n", arr[5]); // Out of bounds! No element at index 5
return 0;
}

⚠️ Possible outcomes:

  • It might print a garbage value
  • It might cause a segmentation fault (crash)
  • Or sometimes, it might appear to “work” — but that’s just luck, not correctness!

💡 Always ensure your loop or index stays between 0 and (size - 1).

Safe Access Example

#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
for (int i = 0; i< 3; i++) {
printf("%d\n", arr[i]);
}
return 0;
}

Expected Output:

10

20

30

✔️ This is safe, correct, and predictable.

📉 (B) Negative Index

Unlike Python, C does not support negative indices.
Trying to access arr[-1] is invalid and leads to undefined behavior.

Example: Negative Index

#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
printf("%d\n", arr[-1]); // ❌ Invalid access
return 0;
}

⚠️ Output:

  • Might print a garbage value
  • Might crash
  • But never guaranteed to do anything meaningful

💡 Moral: Never use negative indices in C.

🧠 (C) Memory Allocation and Array Layout

In C, arrays are stored in contiguous memory.
This means that the elements are placed one after another in memory, making it easy to use pointer arithmetic.

Let’s see this in action 👇

Example: Memory Addresses

#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
printf("%d\n", arr[-1]); // ❌ Invalid access
return 0;
}

💡 Expected Output (may vary by system):

Address of arr[0] = 0x7ffc1234abcd

Address of arr[1] = 0x7ffc1234abcc

Address of arr[2] = 0x7ffc1234abdc

🧠 You’ll notice each address differs by 4 bytes (assuming int = 4 bytes), showing that array elements are stored back-to-back in memory.

That’s why this works:

*(arr + 1) == arr[1]

Because both point to the same memory location.

🧩 Key Takeaways

C arrays do not perform boundary checking — you must stay within valid indices.
Negative indices are invalid and cause undefined behavior.
Arrays are stored in contiguous memory, which is key for understanding pointers and performance.

🧠 Mini Challenge Assignments

🎯Assignment 1:
Write a program that declares an array of size 5, and then:

  • Tries to access arr[7] and arr[-1]
  • Observe and record the outputs
  • Explain what you think happened and why

🎯Assignment 2:
Write a program that prints the address of each element in an array and checks how many bytes apart they are.
(Use %p and compare addresses)

🕵️Bonus: Try printing both arr[i] and *(arr + i) — see if they’re the same! [We will discuss * later on]


Post a Comment

Previous Post Next Post