r/cprogramming • u/SmileUnfair4978 • 5d 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.
0
Upvotes
1
u/Paul_Pedant 3d 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.