由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这题哪错了?
相关主题
c++ 程序一问这个看着很白痴的问题有更好的解法吗?
你们看过programming pearls (2nd edition English) or 正在看的同学们帮看看这段code
one c++ question问一道kth smallest element的题目
C的argc问题leetcode上一题,求正解
为什么我这段简单的程序segment fault写了一个find kth number in 2 sorted arrays的code 请大牛看
bloomberg assessment的机经,c语言的(20道题)一题
C++ online Test 又一题大家能说说(leecode) Permutation Sequence这道题后的数学思路吗?
C++ 一题一道C语言题
相关话题的讨论汇总
话题: int话题: fun话题: daytab话题: array话题: 13
进入JobHunting版参与讨论
1 (共1页)
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?

1 (共1页)
进入JobHunting版参与讨论
相关主题
一道C语言题为什么我这段简单的程序segment fault
懒得写了,想练手的就写写贴在这里吧bloomberg assessment的机经,c语言的(20道题)
问一个c++ 函数指针的问题C++ online Test 又一题
请教一个入门级的C的指针问题C++ 一题
c++ 程序一问这个看着很白痴的问题有更好的解法吗?
你们看过programming pearls (2nd edition English) or 正在看的同学们帮看看这段code
one c++ question问一道kth smallest element的题目
C的argc问题leetcode上一题,求正解
相关话题的讨论汇总
话题: int话题: fun话题: daytab话题: array话题: 13