avatar
C*y
2
指针类型不对
可以int fun(int p[][3])

【在 h*****g 的大作中提到】
: int fun(int **p);
: a[3][3];
: fun(a);
: what's the problem?

avatar
Z*4
3
或者写
int fun(int *p[3])应该也行
avatar
C*y
4
应该是int fun(int (*p)[3])吧

【在 Z**********4 的大作中提到】
: 或者写
: int fun(int *p[3])应该也行

avatar
h*g
5
这样也可以吧?
int fun(int *p);
a[3][3];
fun(*a);

【在 C***y 的大作中提到】
: 指针类型不对
: 可以int fun(int p[][3])

avatar
j*y
6

如果能写成这样,那么就可以写成int fun(int **p)了。
记得int main(int argc, char *argv[])也可以写成int main(int argc, char **argv)
Chevy提供的是正确的用法。
相似的例子在K&R的书上也有:
If a two-dimensional array is to be passed to a function, the parameter
declaration in the function must include the number of columns; the number
of rows is irrelevant, since what is passed is, as before, a pointer to an
array of rows, where each row is an array of 13 ints. In this particular
case, it is a pointer to objects that are arrays of 13 ints. Thus if the
array daytab is to be passed to a function f, the declaration of f would be:
f(int daytab[2][13]) { ... }
It could also be
f(int daytab[][13]) { ... }
since the number of rows is irrelevant, or it could be
f(int (*daytab)[13]) { ... }
which says that the parameter is a pointer to an array of 13 integers. The
parentheses are necessary since brackets [] have higher precedence than *.
Without parentheses, the declaration
int *daytab[13]
is an array of 13 pointers to integers. More generally, only the first
dimension (subscript) of an array is free; all the others have to be
specified.

【在 Z**********4 的大作中提到】
: 或者写
: int fun(int *p[3])应该也行

avatar
n*0
7
fun的声明里说p是一个“pointer to a pointer to an integer”
编译器处理fun(a)调用的时候会把a的l-value(也就是a的首地址,也就是“pointer
to an integer”)传给fun。
所以这里出现了类型的不匹配。

【在 h*****g 的大作中提到】
: int fun(int **p);
: a[3][3];
: fun(a);
: what's the problem?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。