在主空的char * argv的[]参数终止? [英] Are char * argv[] arguments in main null terminated?
问题描述
所以我想知道如果命令行参数总是以空值终止?谷歌似乎要说的是,和编译GCC的指示是这样的话,但我可以保证这始终是真的吗?
INT主(INT ARGC,字符** argv的)
{
字符* P; 对于(INT CNT = 1; CNT< ARGC ++ CNT)
{
P =的argv [CNT]
的printf(%D = [%S] \\ n,CNT,P);
}
返回0;
}$ MyProgram -arg1 -arg2 -arg3
1 = -arg1
2 = -arg2
3 = -arg3
是的。在的argv
阵列指向C字符串,这是由终止定义空的指针。
C语言标准简单地说阵列成员应包含指向字符串(C99§5.1.2.2.1/ 2)。字符串是通过终止字符的连续序列,并包括第一个空字符(C99 7.1.1节/ 1),也就是说,它们是空的定义终止。
此外,在数组元素 ARGV [ARGC]
是空指针,所以数组本身也是,从某种意义上说,空终止。
So I'm wondering if command line parameters are always null terminated? Google seems to say yes, and compiling on GCC indicates this is the case, but can I guarantee this to always be true?
int main(int argc, char** argv)
{
char *p;
for(int cnt=1; cnt < argc; ++cnt)
{
p = argv[cnt];
printf("%d = [%s]\n", cnt, p);
}
return 0;
}
$ MyProgram -arg1 -arg2 -arg3
1 = -arg1
2 = -arg2
3 = -arg3
Yes. The pointers in the argv
array point to C strings, which are by definition null terminated.
The C Language Standard simply states that the array members "shall contain pointers to strings" (C99 §5.1.2.2.1/2). A string is "a contiguous sequence of characters terminated by and including the first null character" (C99 §7.1.1/1), that is, they are null terminated by definition.
Further, the array element at argv[argc]
is a null pointer, so the array itself is also, in a sense, "null terminated."
这篇关于在主空的char * argv的[]参数终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!