DEV Community

Ujjawal Chaudhary
Ujjawal Chaudhary

Posted on

Day 1: The Array Lie (Arrays vs Pointers in C)

Today marks Day 1 of my 30-Day Coding Challenge to master C internals, Algorithms, and Database design.

Why am I doing this? In a world of AI and high-level abstractions, understanding how memory works under the hood is what separates a coder from an engineer. I want to build that foundational strength.

Today's Concept: The Array Lie; We often think of arrays as rigid lists. In C, the variable name of an array is actually just a pointer to the first element.

As shown in the snippet, numbers[1] is really just a human-readable way of writing *(numbers + 1). The computer adds the size of the data type to the base address to find the value.

Follow along for the next 29 days as I document my deep dive into system internals and optimization.

πŸ“‚ View the source code on GitHub: [https://github.com/Ujjawal0711/30-Days/tree/main]

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

... the variable name of an array is actually just a pointer to the first element.

Not quite. For numbers, there is no pointer anywhere in that program nor its memory. For there to be an actual pointer, there would need to be 8 bytes (assuming a 64-bit system) set aside somewhere that contains the address of the array. That's not reality.

The correct way to say it is: the name of an array "decays" into the address of its first element as if it were a pointer to it.

Note that there is an exception: when the name of an array is used as an argument to sizeof, it returns the size of the entire array, not the size of a pointer.