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

查看:130
本文介绍了我应该使用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时,我建议您真正尝试首先理解数组和指针之间的差异,而不是<常见的东西.

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"-它已成为const指针.但是,只有很少的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天全站免登陆