0
  • Linux ミニシェル割り当て用に次のコードを書きましたが、次のエラー メッセージが表示され続けます。- 警告: 互換性のないポインター型から 'strcat' の引数 2 を渡しています - /usr/include/string.h:136:14: 注: 'const char * restrict ' が必要ですが、引数の型は 'char **' です</p >

  • 誰かがそれを調べて、何が悪いのか教えてください。

コード:

int countArgs(char n_args[], ...){
    va_list ap;
    int i, t;
    va_start(ap, n_args);
    for(i=0;t = va_arg(ap, int);i++){ return  t; }
    va_end(ap);
}

char *parse(char buffer[],int num_of_args, char *arguments[])
{ 
    arguments[num_of_args+1]=NULL;
    int i;

    for(i=0;i<num_of_args+1;i++){
        do 
        {
            arguments[i]= strtok(buffer, " ");
        }
        while(arguments!=NULL);
    }

    return arguments;
}

int main(int argc, char **argv)
    char buffer[512];
    char *path = "/bin/";
    while(1)
    {
        //print the prompt
        printf("myShell&gt;");

        //get input
        fgets(buffer, 512, stdin);

        //fork!
        int pid = fork(); //Error checking to see if fork works

        //If pid !=0 then it's the parent
        if(pid!=0)
        {
            wait(NULL);
        }
        else
        {
            //if pid = 0 then we're at teh child
            //Count the number of arguments
            int num_of_args = countArgs(buffer);

            //create an array of pointers for the arguments to be 
            //passed to execcv.
            char *arguments[num_of_args+1];

            //parse the input and arguments will have all the arguments
            // to be passed to the program
            parse(buffer, num_of_args, arguments);                        
            arguments[num_of_args+1] = NULL;

            //This will be the final path to the program that we will pass to execv
            char prog[512];

            //First we copy a /bin/ to prog
            strcpy(prog, path);

            //Then we concancate the program name to /bin/
            //If the program name is ls, then it'll be /bin/ls
            strcat(prog, arguments);

            //pass the prepared arguments to execv and we're done!
            int rv = execv(prog, arguments);
        }
    }
    return 0;
}
4

1 に答える 1

1

strcatの2番目の引数は型const char*ですが、charポインターの配列を渡そうとしています。

すべての配列を文字列に追加する場合は、配列をループして、毎回1​​つの配列項目argumentsで呼び出す必要があります。strcat

for (i=0; i<num_of_args; i++) {
    strcat(prog, arguments[i]);
}

単一の引数を追加する場合は、その配列インデックスを決定して使用します

strcat(prog, arguments[index]);
于 2012-11-01T07:59:44.403 に答える