简体   繁体   English

命令行参数中某些情况下的输出错误

[英]Wrong output for certain cases in command line arguments

I'm required to come up with a C program that accepts up to 2 arguments. 我需要拿出一个C程序,该程序最多可以接受2个参数。 1st argument is a sting, and 2nd argument is a character 'Q'. 第一个参数是一个字符串,第二个参数是一个字符“ Q”。 For example: 例如:

./piglatin Tuesday
uesdayTay
./piglatin Tuesday Q
uesdayTuh
./piglatin
You must enter 1-2 arguments.

I've come up with code that works about 80% of the time, except for when I just enter one argument, then there is additional garbage besides my intended output that's displayed on my screen. 我想出了大约80%的时间都可以工作的代码,除了当我只输入一个参数时,在屏幕上显示的预期输出之外还有其他垃圾。 Also, if my second argument has more than 2 letters, the letters after the first gets printed out in the output. 另外,如果我的第二个参数包含两个以上的字母,则第一个参数之后的字母将输出到输出中。 I can't find out what's wrong with my code, but here it is: 我找不到我的代码出了什么问题,但这是:

#include <stdio.h>
#include <string.h>

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

    if ( argv [1] == NULL )
    {
        printf("You must enter 1-2 arguments\n");
        return 0;
    }

    else if ( argc == 2 && argv [2] == NULL )
    {
        n = strlen ( argv [1] );

        temp = argv [1][0];             
        for ( int j = 1; j <= n-1; j++)         
        {                                   
            argv [1][j-1] = argv [1][j];
        }

        argv [1][n-1] = temp;       
        argv [1][n] = 'a';              
        argv [1][n+1] = 'y';

        printf("%s\n", argv[1]);
        return 0;

    }

    else if ( argc == 3 && argv [3] == NULL )
    {
        if ( *argv [2] == 'Q' )
        {
            n = strlen ( argv [1] );

            temp = argv [1][0];             
            for ( int j = 1; j <= n-1; j++)         
            {                                   
                argv [1][j-1] = argv [1][j];
            }

            argv [1][n-1] = temp;       
            argv [1][n] = 'u';              
            argv [1][n+1] = 'h';

            printf("%s\n", argv[1]);
            return 0;
        }

        else
        {
            n = strlen ( argv [1] );

            temp = argv [1][0];             
            for ( int j = 1; j <= n-1; j++)         
            {                                   
                argv [1][j-1] = argv [1][j];
            }

            argv [1][n-1] = temp;       
            argv [1][n] = 'a';              
            argv [1][n+1] = 'y';

            printf("%s\n", argv[1]);
            return 0;
        }

    }

    else
    {
        printf("You must enter 1-2 arguments\n");
    }


    return 0;
}

Here are the execution traces that are wrong: 这是错误的执行跟踪:

./piglatin Tuesday
uesdayTayANPATH=/soft/jdk1.7.0_76/man:/usr/man:/usr/share/man:/usr/local/man:/usr/X11R6/man

./piglatin R
RayANPATH=/soft/jdk1.7.0_76/man:/usr/man:/usr/share/man:/usr/local/man:/usr/X11R6/man

./piglatin Tuesday QR
uesdayTuhR

The problem, as I see it is with 我认为问题出在

    argv [1][n] = 'a';              
    argv [1][n+1] = 'y';

n-1 is the maximum allowable index to be used. n-1是要使用的最大允许索引。 Accessing out of bound memory causes undefined behavior . 访问超出范围的内存会导致未定义的行为

Same goes for other cases, too. 其他情况也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM