如何将二维数组传递给C中的函数?

编程入门 行业动态 更新时间:2024-10-26 15:26:57
本文介绍了如何将二维数组传递给C中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的函数原型是

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

其中,arr是二维数组,row和col分别是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中的函数?

本文发布于:2023-07-29 19:34:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243455.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何将   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!