C语言 我的代码有什么问题?什么是 argv[1]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16027785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:03:04  来源:igfitidea点击:

What's wrong with my code? What is argv[1]?

cpointersargv

提问by user2203774

I'm trying to ask the user to type in a string so I will print the length of the string. My code is built succeeded. However, when I entered a word and pressed 'enter', the program keeps running. I had to enter a second word, then the length of the first string displays. I'm confused at argv[1]. Can someone give me some tips and hint on how to fix this? Thanks in advance for your time.

我试图让用户输入一个字符串,所以我将打印字符串的长度。我的代码构建成功。但是,当我输入一个单词并按“回车”时,程序会继续运行。我必须输入第二个单词,然后第一个字符串的长度才显示出来。我对 argv[1] 感到困惑。有人可以给我一些提示和提示如何解决这个问题吗?在此先感谢您的时间。

Please note that I'm not allowed to use any string function.

请注意,我不允许使用任何字符串函数。

   int main(int argc, char* argv[]){

    char* s=argv[1];

    char input[256];
    s = input;
    printf("Please enter a string: ");
    scanf("%s\n", s);
    int str_length = 0;
    while (s[str_length] != '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char* argv[]) {
    char input[256];
    int str_length = 0;

    printf ("Please enter a string: ");
    scanf ("%s", input);
    while (input[str_length] != '
a.out 123 abcd
') /* Or consider using strlen() */ str_length++; printf ("%d\n", str_length); return 0; }
') { str_length++; if (s[str_length] == '
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX 100

int main(int argc, char* argv[]){
    char tmp[MAX];
    printf("Plese enter a string:\n");
    scanf("%s", tmp);
    printf("the length is %d\n", strlen(tmp));

    return 0;   
}
') break; } printf("%d\n", str_length); return 0; }

回答by paxdiablo

argv[]is the array that holds your command line parameters, argv[1]is the first one (other than the command representation itself in argv[0], of course). You assign it to sthen immediately overwrite s, so it's not really needed here.

argv[]是保存命令行参数的数组,argv[1]是第一个(argv[0]当然,除了 中的命令表示本身)。您将其分配给sthen 立即覆盖s,因此这里并不真正需要它。

The reason you're having to enter twolines is the \nat the end of your input format. It requires whatever can match the format string followed by a newline, hence the %s\nis eatingyour first newline so that scanfhas to go back for another one.

您必须输入行的原因是\n在输入格式的末尾。它需要任何可以匹配格式字符串后跟换行符的内容,因此它%s\n正在吃掉你的第一个换行符,因此scanf必须返回另一个换行符。

%son it's own will fix that problem but introduce another one if what you're after is a full lineof input - it will only read up to the first whitespace. For a proper line input function, see here.

%s它自己会解决这个问题,但如果你所追求的是完整的输入,则会引入另一个问题- 它只会读取到第一个空格。有关正确的线路输入功能,请参见此处

It does full line input with protection against buffer overflows and detection/cleanup of lines that were too long, something sorely missing in the scanf("%s")input method.

它执行全行输入,防止缓冲区溢出和检测/清理太长的行,这是scanf("%s")输入法中严重缺失的东西。

In addition, the ifstatement within the whileis superfluous here since the whileitself will catch the end of the string, and it makes little sense to have both inputand srefer to the same array (it wouldmake sense if you changed sat some point, but that's not happening here).

此外,这里的if语句while是多余的,因为它while本身会捕获字符串的末尾,并且同时拥有它们inputs引用同一个数组几乎没有意义(如果您在某个时候更改它会有意义s,但那不是发生在这里)。

So, a variant withoutthe advanced line iput function could be as simple as:

因此,没有高级行 iput 函数的变体可能很简单:

int main(int argc, char* argv[]){
    printf("the length is %d\n", strlen(argv[1]));

    return 0;   
}
/*-------------------- Output --------------------
 > a.out 12345
 the length is 5
  ------------------------------------------------*/

If you enter Hello, you'll see that it prints out 5immediately. You'll also see 5if you enter Hello Pax, which is one reason to choose the advanced input function (the other, very important, reason is to avoid introducing a buffer overflow vulnerability into your program).

如果您输入Hello,您会看到它5立即打印出来。您还将看到5是否输入Hello Pax,这是选择高级输入功能的一个原因(另一个非常重要的原因是避免在您的程序中引入缓冲区溢出漏洞)。

回答by Akhilesh Singh

argv is used to pass process arguments. this means you can run your program like

argv 用于传递进程参数。这意味着你可以像这样运行你的程序

scanf("%s", s);

argv[1] will already be assigned to a value of 123. you do not have to read it as you are doing now. in your current code , you are overwriting the reference of s to a new string after assigning it with argv[1].

argv[1] 已经被赋值为 123。您不必像现在这样阅读它。在您当前的代码中,您在使用 argv[1] 分配后将 s 的引用覆盖到新字符串。

回答by Harri Feng

This is what you want

这就是你想要的

##代码##

The argv and argc is another thing like this: argv[0] = a.exe argv[1] = 12345

argv 和 argc 是另一回事: argv[0] = a.exe argv[1] = 12345

##代码##

回答by Beta

Just change the scanfline:

只需更改scanf行:

##代码##

As you have it (scanf("%s\n", s);), it requires a sequnce of characters ending in \n, then another \nto indicate when the input is finished.

正如您所拥有的 ( scanf("%s\n", s);),它需要一个以 结尾的字符序列\n,然后是另一个\n以指示输入何时完成的字符。