问题从argv输出命令行参数 [英] Problem Outputting Command Line Arguments from argv

查看:135
本文介绍了问题从argv输出命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个非常奇怪的问题,我想和你们一起检查




基本上每当我将以下行插入我的程序时

输出传递给程序的参数:


printf(" \\\
Command line arguement%d:%s。",i,argv [i]);


porgramme输出3个命令行参数,然后在下一行给出一个

分段错误,然后是其他奇怪的

行为。


输出命令行参数是否会以某种方式更改它们或其他某些方面的b $ b方面?这个程序运行正常,当我给b $ b评论该线路时,这对我来说没有意义!


亲切的问候,


Matt

Hello. I''m having a very strange problem that I would like ot check
with you guys.

Basically whenever I insert the following line into my programme to
output the arguments being passed to the programme:

printf("\nCommand line arguement %d: %s.", i , argv[i] );

The porgramme outputs 3 of the command line arguements, then gives a
segmentation fault on the next line, followed by other strange
behaviour.

Does outputting the command line arguments change them or some other
aspect of the programme somehow? The programme runs fine when I
comment the line out, which just makes no sense to me!

Kind Regards,

Matt

推荐答案

2007年7月19日星期四下午4:34,Matt< ma ***** @ hotmail.com写在

消息< 11 ********************** @ m3g2000hsh.googlegroups。 com>:
On Thursday 19 Jul 2007 4:34 pm, Matt <ma*****@hotmail.comwrote in
message <11**********************@m3g2000hsh.googlegroups. com>:

你好。我有一个非常奇怪的问题,我想和你们一起检查




基本上每当我将以下行插入我的程序时

输出传递给程序的参数:


printf(" \\\
Command line arguement%d:%s。",i,argv [i]);


porgramme输出3个命令行争论,然后在下一行给出
a分段错误,然后是其他奇怪的

行为。


输出命令行参数是否会以某种方式改变它们或某些其他方面?b $ b这个程序运行正常

我评论该线路,这对我来说没有意义!
Hello. I''m having a very strange problem that I would like ot check
with you guys.

Basically whenever I insert the following line into my programme to
output the arguments being passed to the programme:

printf("\nCommand line arguement %d: %s.", i , argv[i] );

The porgramme outputs 3 of the command line arguements, then gives
a segmentation fault on the next line, followed by other strange
behaviour.

Does outputting the command line arguments change them or some
other aspect of the programme somehow? The programme runs fine when
I comment the line out, which just makes no sense to me!



在C数组中,从下标零开始,以下标N-1结束,对于N元素的

数组。 argv数组是char *的数组。这个数组中的元素数量是由

main的第一个参数给出的,传统上称为argc。因此,当打印出程序

参数时,必须从下标

零到包括下标argc-1遍历argv数组。 argv [argc]是一个null

指针。将空指针传递给大多数期望

有效指针值的函数会导致未定义的行为。

In C arrays begin at subscript zero and end at subscript N-1, for an
array of N elements. The argv array is an array of char *. The
number of elements in this array is given by the first argument to
main, traditionally called as argc. So when printing out the program
arguments, you must iterate through the argv array from subscript
zero up to and including subscript argc-1. argv[argc] is a null
pointer. Passing a null pointer to most functions that expect a
valid pointer value causes undefined behaviour.


Matt< ma ***** @ hotmail.comwrites:
Matt <ma*****@hotmail.comwrites:

你好。我有一个非常奇怪的问题,我想和你们一起检查




基本上每当我将以下行插入我的程序时

输出传递给程序的参数:


printf(" \\\
Command line arguement%d:%s。",i,argv [i]);


porgramme输出3个命令行参数,然后在下一行给出一个

分段错误,然后是其他奇怪的

行为。


输出命令行参数是否会以某种方式更改它们或其他某些方面的b $ b方面?这个程序运行正常,当我用b $ b评论该线路时,这对我来说没有意义!
Hello. I''m having a very strange problem that I would like ot check
with you guys.

Basically whenever I insert the following line into my programme to
output the arguments being passed to the programme:

printf("\nCommand line arguement %d: %s.", i , argv[i] );

The porgramme outputs 3 of the command line arguements, then gives a
segmentation fault on the next line, followed by other strange
behaviour.

Does outputting the command line arguments change them or some other
aspect of the programme somehow? The programme runs fine when I
comment the line out, which just makes no sense to me!



这条线看起来不错(除了你可能应该在产品线的末端和/ b $ b的时候有\ n)你拼写错误的争论,以及行尾的''。''

字符会引起混淆,但那些与你的b $ b相关的不是b $ b真正的问题)。换句话说,你已经告诉我们

你的代码部分*与问题无关

你有。


如果我不得不猜测,我会说你可能有类似的东西:


for(i = 1; i< = argc; i ++){

/ *你的printf在这里打电话* /

}


在最后一次迭代循环,你试图打印值

argc [argc],这保证是一个空指针。


或问题可能是别的完全;我只是猜测。


如果您已经发布了实际代码(复制并粘贴整个程序,

你已经修剪过到了足够大的东西来展示

的问题,但小到足以让我们轻松分析),那么我就不必猜测了.b $ b就不用猜了。 />

只是另一个疯狂的猜测:你还记得所需的

''#include< stdio.h>''?


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

That line looks ok (except that you probably should have the "\n" at
the end of the ouput line and you misspelled "argument", and the ''.''
character at the end of the line can cause confusion, but those aren''t
relevant to your real problem). In other words, you''ve shown us
exactly the portion of your code that *isn''t* relevant to the problem
you''re having.

If I had to guess, I''d say you probably have something like:

for (i = 1; i <= argc; i ++) {
/* your printf call here */
}

On the last iteration of the loop, you attempt to print the value of
argc[argc], which is guaranteed to be a null pointer.

Or the problem might be something else entirely; I''m only guessing.

If you had posted your actual code (copy-and-paste the entire program,
which you''ve trimmed down to something big enough to exhibit the
problem but small enough to be easy for us to analyze), then I
wouldn''t have had to guess.

Just another wild guess: did you remember the required
''#include <stdio.h>''?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


我的相关部分代码如下:


for(i = 0; i< argc; i ++)


{

printf(" \ narg =%d \ n",argc);


printf(" \\\
Command line arguement%d:%s。 \ n",i,* argv [i]);


并在开始时加载stdio.h标头。您可能会注意到我已经在argv [i]之前添加了* *
,因为我意识到我希望打印指针指向的值

,这对于吗?


其他令人担忧的事情是,如果我评论这一点

out,下次程序试图读取argv [1]时,我得到另一个

分段错误。我假设这只发生在argv [1] = NULL,

但是因为我几乎可以确定程序至少提供了一个参数,并且发生了错误当我尝试打印argv [0]

(这是一件不能为空的东西!)时,其他东西必须是

错误。
< br $>
亲切的问候,


Matt
The relevant part of my code reads as follows:

for ( i = 0 ; i < argc ; i++ )

{

printf("\narg = %d\n" , argc );

printf("\nCommand line arguement %d: %s. \n", i , *argv[i] );

and the stdio.h header is loaded at the start. You may notice I have
added a * before the argv[i] as I realised I wished to print the value
the pointer points at, was this a correct thing to do?

Something else that is rather worrying is that if I comment this bit
out, the next time the programme tries to read argv[1], I get another
segmentation fault. I assume this only happens when argv[1] = NULL,
but since I am almost certain the programme to supplied with at least
one argument, and that the error occurs when I try to print argv[0]
(which is the one thing that cannot be NULL!), something else must be
wrong here.

Kind Regards,

Matt


这篇关于问题从argv输出命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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