This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / Another question about pointer in an interview testint array[2][2] = {{1,2}, {3,4}};
void test(void)
{
f(array);
}
How do you declare the function "f" to accept a two-dimensional array of integers as in the above sample code?
a . void f(int*x);
b. void f(int x[]);
c. void f(int**x);
d. void f(int(*x)[]);
e. void f(int*x[]);
-jami(jami);
2005-3-27
{305}
(#2205268@0)
-
c
-bobo123(bobo);
2005-3-27
(#2205752@0)
-
wrong.
-jami(jami);
2005-3-28
(#2206239@0)
-
使用VC 做上边的测试,没有一个能编译通过的.int (*x)[2] 这个是正确的.
-bobo123(bobo);
2005-3-28
{26}
(#2206388@0)
-
dint **t:
* -> * -> ...
int *t[]:
* -> []
[]
[]
[]
...
int (*t):
[]->* ->...
[]->* ->...
[]->* ->...
the last one has compatible pointer type with int[][]
-canadiantire(轮胎-M.I.N.K.);
2005-3-28
{229}
(#2206246@0)
-
Yes. only d is correct. Just tested on Watcom C. This is standard C code.
Also tried on VC. None of them can pass the compiler.
-jami(jami);
2005-3-28
(#2206726@0)
-
a works.void f(int * x);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(int * x)
{
int a,i;
for (i=0;i<=3;i++)
{
a=*(x+i);
printf("%d",a);
}
}
-aka(棒棒);
2005-4-1
{178}
(#2215111@0)
-
With warning --> passing arg 1 of `f' from incompatible pointer type.
-canadiantire(轮胎-M.I.N.K.);
2005-4-1
(#2215125@0)
-
b works.void f(int x[]);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(int x[])
{
int a,i;
for (i=0;i<=3;i++)
{
a=*(x+i);
printf("%d",a);
}
}
~
-aka(棒棒);
2005-4-1
{181}
(#2215129@0)
-
c worksvoid f(int **x);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(int **x)
{
int a,i;
for (i=0;i<=3;i++)
{
a=*(x+i);
printf("%d",a);
}
}
~
-aka(棒棒);
2005-4-1
{181}
(#2215168@0)
-
如果你认为这些指针之间的隐士转换无法通过编译,我建议你去找一本C for Dummy 看看。
-canadiantire(轮胎-M.I.N.K.);
2005-4-1
(#2215172@0)
-
d works. (only one without a warning)void f(int (*x)[]);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(int (*x)[])
{
int i,a;
for (i=0;i<=3;i++)
{
a=*(*x+i);
printf("%d",a);
}
}
-aka(棒棒);
2005-4-1
{181}
(#2215179@0)
-
e works (without a warning)void f(int (*x)[]);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(int (*x)[])
{
int i,a;
for (i=0;i<=3;i++)
{
a=*(*x+i);
printf("%d",a);
}
}
~
~
-aka(棒棒);
2005-4-1
{187}
(#2215194@0)
-
This is not e, this is *D*
-canadiantire(轮胎-M.I.N.K.);
2005-4-1
(#2215560@0)
-
let the E be.void f(int *x[]);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(int *x[])
{
int a,i;
for(i=0;i<=3;i++)
{
a=*(x+i);
printf("%d",a);
}
}
~
-aka(棒棒);
2005-4-1
{180}
(#2216094@0)
-
Why this one works??void f(char ***(**x[][])[]);
main()
{
int arr[2][2]={{1,2},{3,4}};
f(arr);
}
void f(char ***(**x[][])[])
{
int i,a;
for (i=0;i<=3;i++)
{
a=*(int*)x+i;
printf("%d",a);
}
}
~
-aka(棒棒);
2005-4-1
{205}
(#2215198@0)
-
d!
-hippo(最帅的河马);
2005-4-1
(#2215642@0)
-
All above programs are compile under gcc 3.3.1. All give the result of 1234.
-aka(棒棒);
2005-4-1
(#2216099@0)