我应该使用 char** argv 还是 char* argv[]? [英] Should I use char** argv or char* argv[]?

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

问题描述

我刚刚在学习 C 并且想知道我应该在我的主要方法中使用其中的哪一个.有什么区别吗?哪个更常见?

I'm just learning C and was wondering which one of these I should use in my main method. Is there any difference? Which one is more common?

推荐答案

当你刚刚学习 C 时,我建议你真正尝试去理解数组和指针之间的区别,而不是 em>共同的东西.

As you are just learning C, I recommend you to really try to understand the differences between arrays and pointers first instead of the common things.

在参数和数组方面,有一些令人困惑的规则,在继续之前应该清楚.首先,您在参数列表中声明的内容被特殊对待.在某些情况下,在 C 中作为函数参数没有意义.这些是

In the area of parameters and arrays, there are a few confusing rules that should be clear before going on. First, what you declare in a parameter list is treated special. There are such situations where things don't make sense as a function parameter in C. These are

  • 函数作为参数
  • 数组作为参数

第二个可能不是很清楚.但是当您考虑到数组维度的大小是 C 中类型的一部分(并且未给出维度大小的数组具有不完整类型)时,就很清楚了.因此,如果您要创建一个按值接收数组(接收副本)的函数,那么它只能对一个大小执行此操作!此外,数组可能会变大,而 C 试图尽可能快.

The second maybe is not immediately clear. But it becomes clear when you consider that the size of an array dimension is part of the type in C (and an array whose dimension size isn't given has an incomplete type). So, if you would create a function that takes an array by-value (receives a copy), then it could do so only for one size! In addition, arrays can become large, and C tries to be as fast as possible.

在 C 中,由于这些原因,数组值 不存在.如果你想获得一个数组的值,你得到的是一个指向该数组第一个元素的指针.而这里其实已经有了解决方案.C 编译器不会预先绘制无效的数组参数,而是将相应参数的类型转换为指针.记住这一点,非常重要.参数不是数组,而是指向相应元素类型的指针.

In C, for these reasons, array-values are not existent. If you want to get the value of an array, what you get instead is a pointer to the first element of that array. And herein actually already lies the solution. Instead of drawing an array parameter invalid up-front, a C compiler will transform the type of the respective parameter to be a pointer. Remember this, it's very important. The parameter won't be an array, but instead it will be a pointer to the respective element type.

现在,如果您尝试传递数组,则传递的是指向数组第一个元素的指针.

Now, if you try to pass an array, what is passed instead is a pointer to the arrays' first element.

为了完成,并且因为我认为这将有助于您更好地理解问题,让我们看看当您尝试将函数作为参数时的情况.确实,首先它没有任何意义.参数怎么可能是函数?嗯,当然,我们想要在那个地方有一个变量!因此,当这种情况发生时,编译器会再次将函数转换函数指针.尝试传递一个函数将传递一个指向相应函数的指针.因此,以下是相同的(类似于数组示例):

For completion, and because I think this will help you better understand the matter, let's look what the state of affairs is when you try to have a function as a parameter. Indeed, first it won't make any sense. How can a parameter be a function? Huh, we want a variable at that place, of course! So what the compiler does when that happens is, again, to transform the function into a function pointer. Trying to pass a function will pass a pointer to that respective function instead. So, the following are the same (analogous to the array example):

void f(void g(void));
void f(void (*g)(void));

请注意,*g 需要括号.否则,它将指定一个返回 void* 的函数,而不是一个指向返回 void 的函数的指针.

Note that parentheses around *g is needed. Otherwise, it would specify a function returning void*, instead of a pointer to a function returning void.

现在,我在开头说过数组可以有一个不完整的类型——如果你还没有给出大小就会发生这种情况.由于我们已经认为不存在数组参数,而是任何数组参数都是指针,因此数组的大小无关紧要.这意味着,编译器将翻译以下所有内容,并且都是一样的:

Now, I said at the beginning that arrays can have an incomplete type - which happens if you don't give a size yet. Since we already figured that an array parameter is not existent but instead any array parameter is a pointer, the array's size doesn't matter. That means, the compiler will translate all of the following, and all are the same thing:

int main(int c, char **argv);
int main(int c, char *argv[]);
int main(int c, char *argv[1]);
int main(int c, char *argv[42]);

当然,能放进去任何大小的东西没有多大意义,它只是扔掉了.出于这个原因,C99 为这些数字提出了一个新的含义,并允许括号内出现其他内容:

Of course, it doesn't make much sense to be able to put any size in it, and it's just thrown away. For that reason, C99 came up with a new meaning for those numbers, and allows other things to appear between the brackets:

// says: argv is a non-null pointer pointing to at least 5 char*'s
// allows CPU to pre-load some memory. 
int main(int c, char *argv[static 5]);

// says: argv is a constant pointer pointing to a char*
int main(int c, char *argv[const]);

// says the same as the previous one
int main(int c, char ** const argv);

最后两行说你不能在函数内改变argv"——它变成了一个常量指针.尽管只有少数 C 编译器支持这些 C99 特性.但是这些功能清楚地表明数组"实际上并不是一个.这是一个指针.

The last two lines say that you won't be able to change "argv" within the function - it has become a const pointer. Only few C compilers support those C99 features though. But these features make it clear that the "array" isn't actually one. It's a pointer.

请注意,只有当您将数组作为函数的参数时,我上面所说的都是正确的.如果您使用本地数组,则数组将不是指针.它将行为作为一个指针,因为如前所述,当数组的值被读取时,它会被转换为一个指针.但它不应该与指针混淆.

Note that all i said above is true only when you have got an array as a parameter of a function. If you work with local arrays, an array won't be a pointer. It will behave as a pointer, because as explained earlier an array will be converted to a pointer when its value is read. But it should not be confused with pointers.

一个经典的例子如下:

char c[10]; 
char **c = &c; // does not work.

typedef char array[10];
array *pc = &c; // *does* work.

// same without typedef. Parens needed, because [...] has 
// higher precedence than '*'. Analogous to the function example above.
char (*array)[10] = &c;

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

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