繁体   English   中英

我不明白什么是args [0] [0]-'!' 手段

[英]I don't understand what args[0][0]-'!' means

我偶然发现了这段代码,我想了解args[0][0]-'!' 手段?

else if (args[0][0]-'!' ==0)
{   int x = args[0][1]- '0'; 
    int z = args[0][2]- '0'; 

    if(x>count) //second letter check
    {
    printf("\nNo Such Command in the history\n");
    strcpy(inputBuffer,"Wrong command");
    } 
    else if (z!=-48) //third letter check
    {
    printf("\nNo Such Command in the history. Enter <=!9 (buffer size        is 10 along with current command)\n");
    strcpy(inputBuffer,"Wrong command");
    }
    else
    {

        if(x==-15)//Checking for '!!',ascii value of '!' is 33.
        {    strcpy(inputBuffer,history[0]);  // this will be your 10 th(last) command
        }
        else if(x==0) //Checking for '!0'
        {    printf("Enter proper command");
            strcpy(inputBuffer,"Wrong command");
        }

        else if(x>=1) //Checking for '!n', n >=1
        {
            strcpy(inputBuffer,history[count-x]);

        }

    }

这段代码来自这个github帐户: https : //github.com/deepakavs/Unix-shell-and-history-feature-C/blob/master/shell2.c

args是一个char** ,或者换句话说,是一个字符串数组(字符数组)。 所以:

args[0]             // first string in args
args[0][0]          // first character of first string in args
args[0][0]-'!'      // value of subtracting the character value of ! from
                    // the first character in the first string in args
args[0][0]-'!' == 0 // is said difference equal to zero

换句话说,它检查args的第一个字符串是否以!开头! 字符。

它可以(并且应该应该)重写为

args[0][0] == '!'

以及(但不要使用此):

**args == '!'

'!' 只是数值的文本表示形式,它以指定的编码方式对感叹号进行编码。 args[0][0]是数组第一个元素的第一个字符。

那么,当x - y == 0 x == y时,在另一侧移动y ,使代码等效于args[0][0] == '!'

在示例中,我没有任何实际理由将等同表示为减法。

暂无
暂无

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

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