Issue:
In C , passing a multidimensional C-style array to a function expecting an array of int* results in a compiler error.
Reason:
A multidimensional array like int4 is not directly convertible to a pointer of type int*, which is what int arr[] represents in the function declaration.
Example:
#include<stdio.h> void print(int *arr[], int s1, int s2) { // ... } int main() { int a[4][4] = {{0}}; print(a, 4, 4); // Error in C++ }
C will report an error:
cannot convert `int (*)[4]' to `int**' for argument `1' to `void print(int**, int, int)'
Solution:
In both C and C , passing a multidimensional array as int** is not valid. To achieve this effectively, the array must be converted to a pointer using the following technique:
Note:
Ignoring compiler warnings or failing to implement the proper conversion may result in undefined behavior and unpredictable results.
The above is the detailed content of Why Doesn't Passing a C-Style 2D Array to a Function Expecting `int` Work in C ?. For more information, please follow other related articles on the PHP Chinese website!