C *[] 和 ** 之间的区别
这可能是一个基本问题,但是写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
).
在正确的情况下(即,不作为函数参数),使用数组和指针表示法之间可能存在差异.一种常见的方法是在 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