2016-10-12 96 views
0

我偶然发现了这段代码,我想了解什么是 args[0][0]-'!'的意思?我不明白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

+0

'args [0] [0]'是args数组中第一个字符串的第一个字符,所以它将减去'!'的ascii代码从它 – bruceg

+0

这是减去,正是它看起来像 - 你的问题是什么ASCII? –

+0

第一个参数'argv [0]'通常是可执行文件名本身。但AFAIK'exec **'函数不会传递任何程序参数,所以这可能就是这种用法。 –

回答

3

argschar**,或者换言之,一个字符串数组(字符数组)。所以:

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 == '!' 
3

'!'只是一个数值的文本表示在指定的编码中编码感叹号的值。 args[0][0]是数组第一个元素的第一个字符。因此,当x - y == 0?另一边移动y,当x == y时,让代码等于args[0][0] == '!'

虽然我没有看到任何实际的理由来表达等效作为减法。

相关问题