Answers for "How to pass a multidimensional array to a function in C and C++"

C++
0

How to pass a multidimensional array to a function in C and C++

1 #include <stdio.h>
 2 
 3 void print_2d_array(int rows, int cols, int *a) {
 4     for(int i = 0; i < rows; ++i) {
 5         for(int j = 0; j < cols; ++j) {
 6             printf("%d ", a[i * cols + j]);
 7         }
 8         printf("\n");
 9     }
10 }
11 
12 int main(void) {
13     int arr[][3] = {
14         {1, 2, 3},
15         {4, 5, 6}	
16     };
17 
18     print_2d_array(2, 3, arr[0]);
19 }
Posted by: Guest on April-13-2022

Code answers related to "How to pass a multidimensional array to a function in C and C++"

Browse Popular Code Answers by Language