Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

#include<stdio.h> void print(int *arr[], int s1, int s2) {     int i, j;     for(i = 0; i<s1; i++)         for(j = 0; j<s2; j++)             printf("%d, ", *((arr+i)+j)); }  int main() {     int a[4][4] = {{0}};     print(a,4,4); } 

This works in C, but not in C++.

error:

cannot convert `int (*)[4]' to `int**' for argument `1' to  `void print(int**, int, int)' 

Why does it not work in C++? What change is needed to be made?

like image 885
Moeb Avatar asked May 13 '10 16:05

Moeb


People also ask

Can you pass multidimensional array to a function?

In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples. In C++, we can pass arrays as an argument to a function. And, also we can return arrays from a function.

Can you pass two-dimensional array to function in C?

// The following program works only if your compiler is C99 compatible. If compiler is not C99 compatible, then we can use one of the following methods to pass a variable sized 2D array. In this method, we must typecast the 2D array when passing to function.

How do you pass a 3d array to a function?

1 #include <stdio. h> 2 #include <stdlib. h> 3 4 void print_2d_array_of_pointers(int rows, int cols, int **a) { 5 for(int i = 0; i < rows; ++i) { 6 for(int j = 0; j < cols; ++j) { 7 printf("%d ", a[i][j]); 8 } 9 printf("\n"); 10 } 11 } 12 13 // ...

How to pass two dimensional array to a function in C++?

Passing two dimensional array to a C++ function. C++Server Side ProgrammingProgramming. C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index. There are three ways to pass a 2D array to a function: Specify size of columns of 2D array.

How to pass an array to a function in C?

In C programming, a single array element or an entire array can be passed to a function. This can be done for both one-dimensional array or a multi-dimensional array. Single element of an array can be passed in similar manner as passing variable to a function. C program to pass a single element of an array to function.

Can I pass a multidimensional array as a single argument?

In C can I pass a multidimensional array to a function as a single argument when I don't know what the dimensions of the array are going to be? If by "single argument" you mean passing just the array without passing the array dimensions, no you can't. At least not for true multidimensional arrays.

What does it mean when a function takes a two-dimensional array?

This signifies that the function takes a two-dimensional array as an argument. We can also pass arrays with more than 2 dimensions as a function argument. When passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array.


2 Answers

This code will not work in either C or C++. An array of type int[4][4] is not convertible to a pointer of type int ** (which is what int *arr[] stands for in parameter declaration). If you managed to compile it in C, it is simply because you probably ignored a C compiler warning of basically the same format as the error message you got from C++ compiler. (Sometimes C compilers issue warnings for what is essentially an error.)

So, again, don't make assertions that are not true. This code does not work in C. In order to convert a built-in 2D array into a int ** pointer you can use a technique like this one

Converting multidimensional arrays to pointers in c++

(See the accepted answer. The problem is exactly the same.)

EDIT: The code appears to work in C because another bug in the printing code is masquerading the effects of the bug in array passing. In order to properly access an element of an int ** pseudo-array, you have to use expression *(*(arr + i) + j), or better a plain arr[i][j] (which is the same thing). You missed the extra * which made it print something that has absolutely nothing to do with the content of your array. Again, initialize your array in main to something else to see that the results you are printing in C have absolutely nothing to do with the your intended content of the array.

If you change the printf statement as shown above, your code will most likely crash because of the array-passing bug I described initially.

One more time: you cannot pass a int[4][4] array as an int ** pseudo-array. This is what the C++ is telling you in the error message. And, I'm sure, this is what your C compiler told you, but you probably ignored it, since it was "just a warning".

like image 170
AnT Avatar answered Sep 18 '22 14:09

AnT


The problem is, that

int a[4][4]; 

will actually be stored in a physically continuous memory. So, to access an arbitrary part of your 4x4 array, the function "print" needs to know the dimensions of the array. For example the following little piece of code, will access the same part of the memory in two different ways.

#include <iostream>  void print(int a[][4]) {     for (int i = 0; i <4; i++) {         for (int j = 0; j < 4; j++) {             //accessing as 4x4 array             std::cout << a[i][j] <<std::endl;                      //accessing corresponding to the physical layout in memory             std::cout <<  *(*(a)+ i*4 + j) << std::endl;            }     } }  int main() {     int a[4][4];      //populating the array with the corresponding indices from 0 to 15     int m = 0;     for (int i = 0; i<4; i++) {         for (int j= 0; j < 4; j++) {             a[i][j] =  m;             m++;         }     }     print(a); } 

So the memory layout doesn't change but the way of accessing does. It can be visualized like a checkerboard.

   0  1  2  3   ---------- 0| 1  2  3  4 1| 5  6  7  8 2| 9 10 11 12 3|13 14 15 16 

But the real physical memory looks like this.

0*4+0 0*4+1 0*4+2 0*4+3 1*4+0 1*4+1 1*4+2 1*4+3 2*4+1   etc. ----------------------------------------------------- 1      2       3    4     5     6      7     8     9    etc. 

In c++ the data of an array is stored row-by-row and the length of a row (in this case 4) is always necessary to get to the proper memory offset for the next row. The first subscript therefore only indicates the amount of storage that is needed when the array is declared, but is no longer necessary to calculate the offset afterwards.

like image 20
Lucas Avatar answered Sep 19 '22 14:09

Lucas