Isn't the tutorial misleading when it says they both output the data as (graphic), when the graphic is a 2-dimensional array?
|
Be careful with your statements. The tutorial says:
None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way: |
Note: "None [sic] .... produce any output on the screen..."
The purpose of the example is not to show printouts (because there is none), but to show how memory is allocated in a 2-dimensional array. Note the statement further up the tutorial:
Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices: |
The abstraction for programmers is the key. You don't have to use it if you don't want to. If you don't want to use the abstraction, you can use the contiguous memory as a single dimensional array and do the math yourself.
To the computer it's just memory. The C++ compiler gives you the flexibility to look at it as either a 1-dimensional or 2-dimensional array--it's your choice. The example shows you how. The "graphic" that you mention just shows the entire memory used by the (either) array.
So, if you have data in your code that is conceptually a 3 x 5 array (e.g. scores on 5 tests for 3 students, weight for 3 people over 5 weeks, etc.), you can set up either a 1-dimensional or 2-dimensional array to handle it, and both use the same memory. as demonstrated in the examples.
The multi-dimensional array is a nice construct, but sometimes it gets a little cumbersome to use, or the syntax can get a bit hairy. In that case, it makes sense to treat it the memory as a single dimensional array and index into it manually. It depends on the application.
But, as demonstrated by this example, you can do both at the same time. You could have an int*[5] pointer (2-dimensional) (MAKE SURE YOU UNDERSTAND POINTERS!!!) and an int* pointer (1-dimensional) pointing validly to the same memory. You just need to understand how the memory is laid out--and that's what the example is showing you.