二维数组作为函数参数---- missing subscript 错误

出自 http://www.cplusplus.com/forum/beginner/75706/

To quote the C standard:

An incomplete type may only by used when the size of an object of that type is not needed. It is not 
needed, for example, when a typedef name is declared to be a specifier for a structure or union, or 
when a pointer to or a function returning a structure or union is being declared. (See incomplete types 
in 6.2.5.) The specification has to be complete before such a function is called or defined.

6.2.5.22 goes on to say:
An array type of unknown size is an incomplete type. It is completed, for an identifier of 
that type, by specifying the size in a later declaration (with internal or external linkage).

What does that mean?

It means that for:

 
void mat(int param[]);
 
the size is not needed. It can be treated as an array of int of unspecified size where the size isn't needed. Incrementing a pointer to the next element of the array means adding sizeof(int) to the pointer.

For:
 
void mat(int param[][]);
 
a 2D array (an array of int[]) is specified, but we don't know the size of int[]. In this case, the size is needed. We don't know how to increment the pointer to an int[].

For:
 
void mat(int param[][1]);
 
a 2D array (an array of int[1]) is specified. We know how to increment the pointer to the next element, it's sizeof(int) * 1.

Why all this? The background link I posted earlier talks about the history of the treatment of arrays.


编程中,经常需要将一个二维数组作为参数传递给一个函数。当二维数组的行列不确定时,编译会报错。

如果这样定义函数void fun(int matrix[][],int m,int n);则会提示error C2087: '' : missing subscript,因为二维数组作为函数的参数是,二维数组的列值必须确定。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值