我应该使用的char ** argv的或字符* ARGV []用C? [英] Should I use char** argv or char* argv[] in C?

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

问题描述

我只是学习C,想知道这些,我应该在我的主要方法使用哪一个。有什么区别呢?

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

编辑:那么,哪一个更普遍的是使用

So which one is more common to use?

推荐答案

当你刚开始学习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 是必要的。否则,就指定一个函数返回无效* ,而不是指向一个函数返回无效

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

现在,我在这个阵列可以有一个不完整的类型开始说的。既然我们已经想出了一个数组参数没有existant而是任何数组参数是一个指针,数组的大小并不重要。这意味着,编译器将翻译以下所有的,所有的都是同一件事:

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 existant 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的或字符* ARGV []用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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