可以修改argv ??? [英] Is it OK to modify argv ???
问题描述
我正在研究一个有树的程序。命令行参数,
ie,
myprogram level1 [level2 [level3 ...]]]
这样可以超过每个level1的一个level2参数1
参数和每个level2参数的多个level3参数等。
假设我编码它类似于这个片段:
int main(int argc,char * argv [])
{
...
if(strcmp(argv [1]," blivet")== 0)
返回do_blivet( - argc,++ argv);
else if( ...
...
}
int do_blivet(int argc,char * argv []){
...
if(strcmp(argv [1]," whosis")== 0)
return do_whosis( - argc, ++ argv);
else if(...
...
}
实际上,如上所述增加
argv比传递参数(argv + 1)更方便。
我的问题:它是正确的C代码如上所述增加argv?
我想知道这样做是否会阻止内存分配给array * argv []的
在程序退出时被释放,
某些操作系统下的
。
感谢您的建议。
问候,
Charles沙利文
I''m working on a program which has a "tree" of command line arguments,
i.e.,
myprogram level1 [ level2 [ level3 ...]]]
such that there can be more than one level2 argument for each level1
argument and more than one level3 argument for each level2 argument, etc.
Suppose I code it similar to this fragment:
int main( int argc, char *argv[] )
{
...
if ( strcmp(argv[1], "blivet") == 0 )
return do_blivet( --argc, ++argv );
else if ( ...
...
}
int do_blivet( int argc, char *argv[] ) {
...
if ( strcmp(argv[1], "whosis") == 0 )
return do_whosis( --argc, ++argv );
else if ( ...
...
}
It turns out to be somewhat more convenient in practise to increment
argv as above than to pass the argument (argv + 1).
My question: Is it correct C code to increment argv as above?
I''m wondering whether doing this might prevent memory
allocated for array *argv[] from being freed upon program exit,
under some operating systems.
Thanks for your advice.
Regards,
Charles Sullivan
推荐答案
Charles Sullivan< cw ****** @ triad.rr。 COM>在消息中写道
news:pa ************************* @ triad.rr.com ...
Charles Sullivan <cw******@triad.rr.com> wrote in message
news:pa*************************@triad.rr.com...
我正在研究一个有树的程序。命令行参数,
myprogram level1 [level2 [level3 ...]]]
这样每个level1
参数可以有多个level2参数每个level2参数的多个level3参数等。
假设我将其编码为与此片段类似:
int main(int argc,char * argv [])
{
...
if(strcmp(argv [1]," blivet")== 0)
返回do_blivet( - argc,++ argv);
如果(...
...
}
int do_blivet(int argc,char * argv []){
...
if(strcmp(argv [1]," whosis")== 0)
返回do_whosis( - argc,++ argv);
if if(...
...
在实践中,如上所述增加
argv比传递参数(argv + 1)更方便。
我的问题:如上所述增加argv的C代码是否正确?
我想知道这样做是否可能分配给array * argv []的事件内存在程序退出时被释放,
在某些操作系统下。
感谢您的建议。
I''m working on a program which has a "tree" of command line arguments,
i.e.,
myprogram level1 [ level2 [ level3 ...]]]
such that there can be more than one level2 argument for each level1
argument and more than one level3 argument for each level2 argument, etc.
Suppose I code it similar to this fragment:
int main( int argc, char *argv[] )
{
...
if ( strcmp(argv[1], "blivet") == 0 )
return do_blivet( --argc, ++argv );
else if ( ...
...
}
int do_blivet( int argc, char *argv[] ) {
...
if ( strcmp(argv[1], "whosis") == 0 )
return do_whosis( --argc, ++argv );
else if ( ...
...
}
It turns out to be somewhat more convenient in practise to increment
argv as above than to pass the argument (argv + 1).
My question: Is it correct C code to increment argv as above?
I''m wondering whether doing this might prevent memory
allocated for array *argv[] from being freed upon program exit,
under some operating systems.
Thanks for your advice.
请记住,C参数是按值传递的。
所以你对函数参数的任何改变
将不被来电者反映。 ''main()''是
与这方面的任何其他功能没什么不同。
-Mike
Remember that C arguments are passed ''by value''.
So any changes you make to a function''s parameters
will not be reflected by the caller. ''main()'' is
no different from any other function in this respect.
-Mike
Charles Sullivan写道:
Charles Sullivan wrote:
我正在研究一个有树的程序。命令行参数,
int main(int argc,char ** argv)
{
它没关系修改argv,因为它只是一个指针的副本。
argv ++; / *这里没问题* /
如果存在这样的字符,可以修改argv [i] [n]。因此:
if(strlen(argv [1])> strlen(" hello")
{
strcpy (argv [1],你好); / *这里没问题* /
}
修改指针是不行的argv [i]:
argv [i] ="新指针值" ;; / * BUG!* /
结果证明有点多在练习中如上所述增加
argv比传递参数(argv + 1)更方便。
我的问题:如上所述增加argv的C代码是否正确?
是的,没关系。
我想知道这样做是否会阻止分配给array * argv []的内存被释放出程序退出,
在一些操作系统下。
I''m working on a program which has a "tree" of command line arguments,
int main(int argc, char **argv)
{
it''s okay to modify argv, because it''s just a copy of a pointer.
argv++; /* no problem here */
it''s okay to modify argv[i][n], provided such a character exists. Thus:
if(strlen(argv[1]) > strlen("hello")
{
strcpy(argv[1], "hello"); /* no problem here */
}
it''s NOT okay to modify the pointer argv[i]:
argv[i] = "new pointer value"; /* BUG! */
It turns out to be somewhat more convenient in practise to increment
argv as above than to pass the argument (argv + 1).
My question: Is it correct C code to increment argv as above?
Yes, that''s fine.
I''m wondering whether doing this might prevent memory
allocated for array *argv[] from being freed upon program exit,
under some operating systems.
只要你遵守我上面概述的规则(以及所有其他规则,
还有一些我还没想到的事情,你会没事的。
-
Richard Heathfield:< a href =mailto:bi **** @ eton.powernet.co.uk> bi **** @ eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
As long as you stick to the rules I outlined above (and all the other rules,
and some more that I haven''t thought of yet), you''ll be fine.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Charles Sullivan< cw ****** @ triad.rr.com>写道:
< SNIP>
Charles Sullivan <cw******@triad.rr.com> wrote:
<SNIP>
int do_blivet(int argc,char * argv []){
...
if(strcmp(argv [1]," whosis")== 0)
返回do_whosis( - argc,++ argv);
if if(...
argv比传递参数(argv + 1)更方便。
我的问题:如上所述增加argv的C代码是否正确?
我想知道这样做是否会阻止分配给array * argv []的内存被释放程序退出,在某些操作系统下。
int do_blivet( int argc, char *argv[] ) {
...
if ( strcmp(argv[1], "whosis") == 0 )
return do_whosis( --argc, ++argv );
else if ( ...
...
}
It turns out to be somewhat more convenient in practise to increment
argv as above than to pass the argument (argv + 1).
My question: Is it correct C code to increment argv as above?
I''m wondering whether doing this might prevent memory
allocated for array *argv[] from being freed upon program exit,
under some operating systems.
argc和argv有函数范围,IOW它们是main()的本地;
因此,你可以随意改变自己的价值而不会对主要()之外发生的任何事情造成伤害。
问候,
Irrwahn
-
不写:void main(...)
不要使用gets()
不要强制转换malloc的值()
不要fflush(stdin)
读取clc-faq: http://www.eskimo.com /~scs/C-faq/top.html
这篇关于可以修改argv ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!