二维数组作为函数的参数 [英] 2D-array as argument to function

查看:103
本文介绍了二维数组作为函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么你不能像函数一样在函数中声明一个二维数组参数?

  void F (int bar []){} // Ok 
void Fo(int bar [] [])// Not ok
void Foo(int bar [] [SIZE])// Ok $ b $为什么需要声明列的大小?

bar c> c> 衰减到指针。想象下面的代码:

  void Foo(int bar [] [])//伪代码
{
bar ++; //编译器不能知道多少增加指针
//因为它不知道* bar的大小
}

因此,编译器必须知道 * bar 的大小,因此必须提供最右边数组的大小。


Why can't you declare a 2D array argument in a function as you do with a normal array?

 void F(int bar[]){} //Ok
 void Fo(int bar[][]) //Not ok
 void Foo(int bar[][SIZE]) //Ok

Why is it needed to declare the size for the column?

解决方案

You can't write void Foo(int bar[][]), because bar decays to a pointer. Imagine following code:

void Foo(int bar[][]) // pseudocode
{
    bar++; // compiler can't know by how much increase the pointer
    // as it doesn't know size of *bar
}

So, compiler must know size of *bar, therefore size of rightmost array must be provided.

这篇关于二维数组作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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