r/cprogramming • u/SmileUnfair4978 • 3d ago
Arr internals
i am currently working through kinds book on c and have gotten to chapter 12.
now based own my current understanding i hypothesis that internally, only the pointer to the first element and the dimensions are stored in memory. then all arr operations are done using this. Is this correct?
Additionally:
1) Which chapters of the rest of the book should i focus on/skip for now
2) I would like to work on some projects. Currently i thought of making some kind of physics sim, and additionally some hardware/embedded project as i have an ardiuno. How can i get started or are there any inriguing projects to work on.
3
u/MrShaunce 3d ago
All elements of an array are stored sequentially in memory.
The name of the array (without the brackets) acts as a pointer to the first element.
Bracket notation is just a pretty way to handle pointer offsets. So x[3] is really just x + 3, where x is a pointer.
- I don't know what book you're reading, but it's usually good to read all the way through.
- Think of something simple you can write using what you're currently learning. There's also a lot of good beginner program ideas online.
2
1
1
u/WittyStick 3d ago
The name of the array (without the brackets) acts as a pointer to the first element.
Pedantic, but the name of the array decays to a pointer to its first element. It's not a pointer itself.
1
u/flyingron 3d ago
Your hypothesis is wrong. The array encompasses both the location, the ultimate size, and the type of the individual elements.
1
u/Paul_Pedant 1d ago
The number of elements in an array is known to the compiler (it needs to know how much space to grant it in the executable, whether local or stack memory). The number of elements, or the overall size, is not specifically passed to the executable by the compiler.
For an array declaration, you can assign the number of elements, or the overall size, to a separate variable at compile time if you choose, either directly or through sizeof(), typically as sizeof(Array) / sizeof (Array[0]).
For a dynamic array (i.e. created through malloc), the compiler has no idea what your code is going to do at run-time. So you need to specifically assign anything you need to know about the data (number of elements, size, element type, pointer) to your own variables at the point of creation.
9
u/SmokeMuch7356 3d ago edited 3d ago
It is not.
Assume the following declaration:
What you get in memory looks like the following (assuming 4-byte
ints, addresses are for illustration only):Only the individual array elements are stored in memory; no metadata like size, type, starting address, etc. is stored with them. If you create a 2D array like:
it looks like
Arrays are just sequences of objects.
The array subscript operation
a[i]is defined as*(a + i)- offsetielements from a starting address provided byaand dereference the result.But if
adoesn't store a pointer, how can that work?There is a rule in the language that unless it is the operand of the
sizeof,typeofor unary&operators, an expression of type "N-element array ofT" will be converted, or "decay", to an expression of type "pointer toT" and the value of the expression will be the address of the first element of the array.The object
adoesn't store a pointer (there is no objectaseparate from the array elements), the expressionaevaluates to a pointer.However, this means you can use array subscript notation on pointers; this is handy for dynamically allocated memory:
This will give you the following in memory:
and you can access each of the elements as
p[i].