Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array to a function (and why it does not work in C++)

Tags:

c++

c

I have come across some C code that compiles, but I do not understand why. Specifically, I have a C library that has a lot of code using this format:

void get_xu_col(int i_start,                 int n,                 double x[n],                 int n_x,                 int n_u,                 int n_col,                 double xu_col[n_col][n_x + n_u]){     ...  }  int main(){     ...     double xu_col[n_col][n_x + n_u];     get_xu_col( ..., xu_col );     ... } 

What I don't understand is why the compiler allows for sizing in the arrays. To the best of my understanding, either the sizes must be fixed (e.g. xu_col[9][7]) or undefined (e.g. xu_col[][]). In the above code, it appears that the sizes are not compile-time constants.

Is the compiler just ignoring the arguments here? or is it really doing a compile-time check on the dimensions?

If it is the latter, then it seems error-prone to pass the dimensions separately.

The second part of the question is:

Why doesn't the same version work in C++? When I literally change the file extension from .c to .cpp and try to recompile, I get

candidate function not viable: no known conversion from 'double [n_col][n_x + n_u]' to 'double (*)[n_x + n_u]' for 7th argument void get_xu_col(int i_start, int n, double x[n], int n_x, int n_u, int n_col, double xu_col[n_col][n_x + n_u]); 

I would like to know which idiom I should use to convert this code to C++, since apparently the previous idiom was something that works in C, but not C++.

like image 679
bremen_matt Avatar asked Feb 21 '18 08:02

bremen_matt


People also ask

What happens when you pass an array to a function in C?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.

Why Cannot arrays be passed by values to functions?

Arrays are not passed by value because arrays are essentially continuous blocks of memmory. If you had an array you wanted to pass by value, you could declare it within a structure and then access it through the structure.

Can we use array in function in C?

An array can be passed to functions in C using pointers by passing reference to the base address of the array and similarly, a multidimensional array can also be passed to functions in C.


2 Answers

In C, it is possible to use function parameters to define the size of a variable length array parameter as long as the size comes before the array in the parameter list. This is not supported in C++.

like image 199
Stephen Docy Avatar answered Sep 21 '22 09:09

Stephen Docy


The reason it works in C, but not in C++ is simply because it's C code and not C++. The two languages share a history, not a grammar.

The C++ method to pass variable-sized arrays is std::vector, probably by reference if you intend to modify the vector in the function, or by const reference if you don't.

like image 31
MSalters Avatar answered Sep 24 '22 09:09

MSalters