h*****g 发帖数: 312 | 1 int fun(int **p);
a[3][3];
fun(a);
what's the problem? |
C***y 发帖数: 2546 | 2 指针类型不对
可以int fun(int p[][3])
【在 h*****g 的大作中提到】 : int fun(int **p); : a[3][3]; : fun(a); : what's the problem?
|
Z**********4 发帖数: 528 | 3 或者写
int fun(int *p[3])应该也行 |
C***y 发帖数: 2546 | 4 应该是int fun(int (*p)[3])吧
【在 Z**********4 的大作中提到】 : 或者写 : int fun(int *p[3])应该也行
|
h*****g 发帖数: 312 | 5 这样也可以吧?
int fun(int *p);
a[3][3];
fun(*a);
【在 C***y 的大作中提到】 : 指针类型不对 : 可以int fun(int p[][3])
|
j***y 发帖数: 2074 | 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])应该也行
|
n*******0 发帖数: 2002 | 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?
|