* []和**之间的C差异 [英] C difference between *[] and **
问题描述
这可能是一个基本问题,但是写char * []和char **有什么区别?例如,在main中,我可以有一个char * argv [].另外,我可以使用char ** argv.我认为这两种表示法之间肯定会有某种区别.
This might be a bit of a basic question, but what is the difference between writing char * [] and char **? For example, in main,I can have a char * argv[]. Alternatively I can use char ** argv. I assume there's got to be some kind of difference between the two notations.
推荐答案
在这种情况下,没有任何区别.如果您尝试将数组类型用作函数参数,则编译器会将其调整"为指针类型(即T a[x]
作为函数参数表示完全 >与T *a
)相同.
Under the circumstances, there's no difference at all. If you try to use an array type as a function parameter, the compiler will "adjust" that to a pointer type instead (i.e., T a[x]
as a function parameter means exactly the same thing as: T *a
).
在适当的情况下(即, not 作为函数参数),使用数组和指针表示法之间可能会有区别. extern
声明是一种常见的声明.例如,假设我们有一个文件,其中包含以下内容:
Under the right circumstances (i.e., not as a function parameter), there can be a difference between using array and pointer notation though. One common one is in an extern
declaration. For example, let's assume we have one file that contains something like:
char a[20];
,我们想在另一个文件中显示该内容.这将起作用:
and we want to make that visible in another file. This will work:
extern char a[];
但这不会:
extern char *a;
如果我们改为使用指针数组:
If we make it an array of pointers instead:
char *a[20];
...同样如此-声明一个extern数组很好,但是声明一个extern指针却不能:
...the same remains true -- declaring an extern array works fine, but declaring an extern pointer does not:
extern char *a[]; // works
extern char **a; // doesn't work
这篇关于* []和**之间的C差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!