Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save krysseltillada/484112ccff486dd708c5 to your computer and use it in GitHub Desktop.

Select an option

Save krysseltillada/484112ccff486dd708c5 to your computer and use it in GitHub Desktop.
printing values from 2d array using range for, subscripts and pointers
#include <iostream>
int main()
{
int ia[3][4] = {
{0, 1 ,2 ,3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
/// using range for
for (int (&i)[4] : ia) {
for(int &i2 : i) {
std::cout << i2 << " ";
}
std::cout << std::endl;
}
/// using subscript
for (size_t i = 0; i != 3; ++i) {
for (size_t i2 = 0; i2 != 4; ++i2) {
std::cout << ia[i][i2] << " ";
}
std::cout << std::endl;
}
/// using pointers
for (int (*p)[4] = ia; p != std::end(ia); ++p) {
for (int *q = (*p); q != std::end(*p); ++q) {
std::cout << *q << " ";
}
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment