D
D
Danil Tunev2019-11-04 09:02:16
C++ / C#
Danil Tunev, 2019-11-04 09:02:16

How to pass a two-dimensional static array to a C function?

There is an array char mass[1024][1024]and a function int fun(char*). When inserting a function fun(mass), the compiler issues a warning about type mismatch, you have to type cast fun((char*)mass){}or fun((char*)&mass){}. Is it possible to specify a static array as an argument? And why are the types different?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wisgest, 2019-11-04
@lada-guy

The whole array is not passed to the function (unless it is wrapped in a structure), but a pointer to its 0th element is passed, and the formal argument of the function must be a pointer to the type of array elements passed by the actual argument.
An n -dimensional array is a 1 - dimensional array of (n-1) -dimensional arrays and, accordingly, the formal argument of the function must be a pointer to an (n-1)-dimensional array.
In this case

int fun(char (*)[1024]);
int fun(char (*m)[1024]) {/*...*/}

J
jcmvbkbc, 2019-11-04
@jcmvbkbc

There is an array char mass[1024][1024] and a function int fun(char*)

can be passed to such a function fun(&mass[0][0]).
In the sense of an array of static dimension? Easily:int fun(char a[][1000]); ... fun(mass);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question