如何将二维数组传递给C中的函数? [英] How to pass a two-dimensional array to a function in C?

查看:99
本文介绍了如何将二维数组传递给C中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数原型是

int** rotate(int **arr, int row, int col, int fl);

其中,arr是二维数组,rowcol分别是2D数组的行数和列数,fl是标志变量.如果fl的值是0,则该数组将向右旋转;如果fl是1,则该数组将向左旋转.

where arr is the two dimensional array, row and col is the number of row and columns of the 2D array respectively, fl is a flag variable. If the value of fl is 0 then the array will be rotated right, if fl is 1 then the array will be rotated left.

我调用了以下函数:

int **res= rotate(arr, row, col, fl);

但是我得到一个警告和一个音符

But I got one warning and one note

[Warning] passing argument 1 of 'rotate' from incompatible pointer type.

[Note] expected 'int **' but argument is of type 'int (*)[20]' 

推荐答案

指向指针的指针与指向数组的指针不同.数组到指针的衰减只能发生在最左侧(例如int [3][20]int (*)[20]).

A pointer to a pointer is different from a pointer to an array. Array-to-pointer decaying can only happen on the left-most side (e.g. int [3][20] to int (*)[20]).

将函数声明更改为

int** rotate(int (*arr)[20], int row, int col, int fl);

或更明显的是

int** rotate(int arr[][20], int row, int col, int fl);

请注意,您必须在编译时固定大小.

Note you have to fix the size at compile-time.

这篇关于如何将二维数组传递给C中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆