fork download
  1. #include <cstddef>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. // Prints the array contents to std::cout.
  6. template <typename T, std::size_t R, std::size_t C>
  7. void printArr(const T (&arr)[R][C]) {
  8. for (auto& r : arr) {
  9. for (auto& c : r) {
  10. std::cout << c << " ";
  11. }
  12. std::cout << std::endl;
  13. }
  14. }
  15.  
  16. int main() {
  17. const std::size_t rows = 5;
  18. const std::size_t cols = 5;
  19. char arr[rows][cols]; // Create the array.
  20.  
  21. // Populate array with '-'.
  22. for (auto& r : arr) {
  23. for (auto& c : r) {
  24. c = '-';
  25. }
  26. }
  27.  
  28. // Set first column to 'hello'.
  29. std::string s{"hello"};
  30. for (std::size_t i = 0; i != s.size(); ++i) {
  31. arr[i][0] = s[i];
  32. }
  33.  
  34. // Print array to screen.
  35. printArr(arr);
  36. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
h - - - - 
e - - - - 
l - - - - 
l - - - - 
o - - - -