(C++) Printing Out 2d Array
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".
But that would require casting to int* first, otherwise it will access the row (y * 5) + x.
If you really want to fry your noodle, you'll print your array like this:
for (int i=0; i<25; ++i) std::cout << i[*map] << " \n"[i%5];
How would those lines work?
map[(y * 5) + x] = ...
seems like y is pointing to 5, which would just slow things down by the looks of it.
And:
for (int i=0; i<25; ++i) std::cout << i[*map] << " \n"[i%5];
How does i[*map] work?
i points to map?
map[(y * 5) + x] = ...
seems like y is pointing to 5, which would just slow things down by the looks of it.
And:
for (int i=0; i<25; ++i) std::cout << i[*map] << " \n"[i%5];
How does i[*map] work?
i points to map?
Unless operator[] has been overridden, a is really short-hand notation for *(a+b). Typically a is a pointer and b is an integer, but there is nothing preventing you from reversing the order. Now *map is a pointer to the first row of the matrix, and I am abusing it to access the whole matrix.
The " \n"[i%5] is a bit more straight forward: It evaluates to a space unless i%5==4, which is when you need to print a '\n'. So it separates elements in a row and prints new-line characters all in one little expression.
The " \n"[i%5] is a bit more straight forward: It evaluates to a space unless i%5==4, which is when you need to print a '\n'. So it separates elements in a row and prints new-line characters all in one little expression.
[quote name='MarkS' timestamp='1354902146' post='5008178']
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".
But that would require casting to int* first, otherwise it will access the row (y * 5) + x.
[/quote]
DOH! And there I was trying to look smart...
[quote name='ultramailman' timestamp='1354913962' post='5008227']
[quote name='MarkS' timestamp='1354902146' post='5008178']
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".
But that would require casting to int* first, otherwise it will access the row (y * 5) + x.
[/quote]
DOH! And there I was trying to look smart...
[/quote]
Heh, don't we all? My first post in this thread was also a failed attempt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement