fork download
  1. #include <iostream>
  2.  
  3. #define N 2
  4. #define M 3
  5.  
  6. int main() {
  7. int matrix[N][M] = {{1, 2, 3}, {4, 5, 6}};
  8.  
  9. // First version
  10. // int array[N * M];
  11. // for (int y = 0; y < N; ++y) {
  12. // for (int x = 0; x < M; ++x) {
  13. // array[y * M + x] = matrix[y][x];
  14. // }
  15. // }
  16.  
  17. // Second version
  18. int* array;
  19. array = (int*)matrix;
  20.  
  21. for (int i = 0; i < N * M; ++i)
  22. std::cout << array[i] << " ";
  23.  
  24. std::cout << std::endl;
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6