char ** argv与char * argv [] [英] char **argv vs. char* argv[]

查看:64
本文介绍了char ** argv与char * argv []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么char ** argv在main()声明中是可以接受的。


在comp.lang.c FAQ(问题6.18)中它说指针

指针和指向数组的指针不可互换。然而

声明:


int main(int argc,char ** argv)


很常见。


如何解决这个问题?编译器不应该抱怨,如果你不是这样做的话:


int main(int argc,char * argv [])


谢谢,

布雷特

I''m curious why char** argv is acceptable in the main() declaration.

In the comp.lang.c FAQ (question 6.18) it says that pointers to
pointers and pointers to an array are not interchangable. However the
declaration:

int main(int argc, char** argv)

is common.

How does that work out? Shouldn''t the compiler complain if you''re not
doing:

int main(int argc, char* argv[])

Thanks,
Bret

推荐答案

Bret< bl *** @ yahoo .COM>潦草地写道:
Bret <bl***@yahoo.com> scribbled the following:
我很好奇为什么char ** argv在main()声明中是可以接受的。
在comp.lang.c FAQ(问题6.18)中,它指出指向数组的指针和指针是不可互换的。但是
声明:
int main(int argc,char ** argv)
很常见。
这是怎么回事?编译器不应该抱怨你是不是在做:
int main(int argc,char * argv [])
I''m curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to
pointers and pointers to an array are not interchangable. However the
declaration: int main(int argc, char** argv) is common. How does that work out? Shouldn''t the compiler complain if you''re not
doing: int main(int argc, char* argv[])




这是因为在函数参数声明中,char ** foo和

char * foo []是一回事。不仅在main()中,而且在其他每个

函数中也是如此。更重要的是,int ** foo和int * foo []也是一样的。

所以是int *** foo和int ** foo []。

实际上,一般来说,如果T是一个类型,那么T * foo和T foo []就是函数参数声明中的相同的东西。 (这只是一个

级别的深度 - T ** foo和T foo [] []完全是另一回事!)

这一切都是由Chris Torek解释的是规则。如果用作值,

或作为函数参数,则数组会衰减成指向其第一个元素的指针。因此,你根本无法将数组传递给函数

。您可以将指针传递给包含

数组的数组或结构,但不能传递数组本身。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

玫瑰是红色的,紫罗兰是蓝色的,我是schitzophrenic,我也是。

- Bob Wiley



This is because in function parameter declarations, char **foo and
char *foo[] are the same thing. Not just in main(), but in every other
function too. What''s more, int **foo and int *foo[] are also the same.
So are int ***foo and int **foo[].
In fact, in general, if T is a type, then T *foo and T foo[] are the
same thing in function parameter declarations. (This goes ONLY one
level deep - T **foo and T foo[][] are a different matter entirely!)
This is all explained by Chris Torek''s THE RULE. If used as a value,
or as a function parameter, an array decays into a pointer into its
first element. Therefore you can''t really pass arrays into functions
at all. You can pass pointers to arrays, or structures containing
arrays, but not arrays themselves.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Roses are red, violets are blue, I''m a schitzophrenic and so am I."
- Bob Wiley


文章< a1 ************************** @ posting.postoutgoogle.com>,

Bret< bl *** @ yahoo.com>写道:
In article <a1**************************@posting.google.com >,
Bret <bl***@yahoo.com> wrote:
我很好奇为什么char ** argv在main()声明中是可以接受的。

在comp.lang.c FAQ(问题6.18)中说指向指针和指向数组的指针是不可互换的。但是声明:

int main(int argc,char ** argv)

很常见。
I''m curious why char** argv is acceptable in the main() declaration.

In the comp.lang.c FAQ (question 6.18) it says that pointers to
pointers and pointers to an array are not interchangable. However the
declaration:

int main(int argc, char** argv)

is common.




char ** argv将argv声明为指向字符指针的指针

char * argv []将argv声明为字符指针数组


在任何一种情况下都没有指向数组的指针。因此,指向数组的指针是否与指针指针可互换是否与char ** argv无关。和char * argv []

允许作为main()的第二个参数。


您想要问的相关问题是数组和

指针是可互换的。它们不是一般情况下,在一个函数的

形式参数列表中,它们或多或少是对最外层嵌套的b / b
层。


- Brett



char **argv declares argv as a pointer to a pointer to a character
char *argv[] declares argv as am array of pointers to a character

There are no pointers to arrays in either case. So whether or not
pointers to arrays are interchangable with pointers to pointers is
irrelevant to whether "char **argv" and "char *argv[]" are both
allowable as the second parameter to main().

The relevant question you would want to ask is whether arrays and
pointers are interchangable. They aren''t in the general case, in the
formal parameter list of a function, they more-or-less are with respect
to the outer-most nesting layer.

-- Brett


Bret写道:
Bret wrote:
我''我很好奇为什么char ** argv在main()声明中是可以接受的。

在comp.lang.c常见问题解答(问题6.18)中它表示
指向指针和指针的指针数组不可互换。
然而声明:

int main(int argc,char ** argv)

很常见。
如何解决这个问题?
如果你没做的话,编译器不应该抱怨:

int main(int argc,char * argv [])


它可能会抱怨。

cat fc
int f(char * s []){

char ** p = s;

}

gcc -Wall -std = c99 -pedantic -c fc
fc:函数`f'':

fc:2:警告:未使用的变量`p''

fc:3:警告:控制到达无效功能的结束


C执行隐式转换

从数组到指向其第一个元素的指针

和指向数组的指针。


我可以写

cat fc
int f(char * s []){

char ** p = s;

return f(p);

}

gcc -Wall -std = c99 -pedantic -c fc
I''m curious why char** argv is acceptable in the main() declaration.

In the comp.lang.c FAQ (question 6.18) it says that
pointers to pointers and pointers to an array are not interchangeable.
However the declaration:

int main(int argc, char** argv)

is common.

How does that work out?
Shouldn''t the compiler complain if you''re not doing:

int main(int argc, char* argv[])
It probably should complain.
cat f.c int f(char* s[]) {
char** p = s;
}
gcc -Wall -std=c99 -pedantic -c f.c f.c: In function `f'':
f.c:2: warning: unused variable `p''
f.c:3: warning: control reaches end of non-void function

C performs an "implicit conversion"
from an array to a pointer to its first element
and from a pointer to an array.

I can write
cat f.c int f(char* s[]) {
char** p = s;
return f(p);
}
gcc -Wall -std=c99 -pedantic -c f.c




我的编译器不会抱怨。



and my compiler will not complain.


这篇关于char ** argv与char * argv []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆