【发布时间】:2020-08-28 02:08:57
【问题描述】:
我正在查看这个问题Why is it allowed to omit the first dimension, but not the other dimensions when declaring a multi-dimensonal array? 的答案以及其他一些问题,我知道我们可以省略第一个维度,但其他维度是不可避免的。
令我惊讶的是,以下代码执行得非常好:
#include<stdio.h>
void f1(int (*p)[]){
//SOMETHING
}
int main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
f1(a);
}
但是,如果我使用指针 p 打印一些值,例如
printf("%d",p[0][1]);
它给出了一条错误消息:
error: invalid use of array with unspecified bounds
为什么 C 允许这样的声明?
如果非常确定它会在使用指针时抛出错误,那么为什么它在声明本身时不抛出错误?为什么要等待指针使用抛出错误?
允许这样的声明有什么具体原因吗?
【问题讨论】:
标签: c pointers multidimensional-array pointer-to-array