C字符数组指针不是有效语句

时间:2014-12-11 02:59:04

标签: c arrays unix pointers char

我们得到了一个赋值,它将从终端接收两个命令并将第一个命令传送到第二个命令。我相信我的代码结构是正确的,但是,在尝试编译它时会抱怨我的初始char * cmd不是一个有效的语句。为什么会这样?我很困惑,任何帮助都会受到赞赏。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void main(int argc, char *argv[])
{
    int f_des[2];

    if(pipe(f_des) == -1)
    {
        perror("pipe");
        exit(1);
    }

    switch(fork())
    {
        case -1: perror("fork");
            exit(2);
            case 0: dup2(f_des[1], fileno(stdout));
            close(f_des[0]);
            close(f_des[1]);
            char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};
            char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
            int ret;
            ret = execvp(cmd,path);
            exit(3);
        case default: dup2(f_des[0], fileno(stdin));
            close(f_des[0]);
            close(f_des[1]);
            char *cmd[] = {"/usr/bin/"+argv[2], argv[2], (char *)0};
            char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
            int ret;
            ret = execvp(cmd,path);
            exit(4);
    }
}

错误:

cc: Error: lab1.c, line 22: Invalid statement.
char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};
^
cc: Error: lab1.c, line 22: Missing ";".
char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};
-----------------------------------------------------^
cc: Error: lab1.c, line 23: Invalid statement.
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
^
cc: Error: lab1.c, line 23: Missing ";".
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
------------------------------------------------------------^
cc: Error: lab1.c, line 24: Invalid statement.
int ret;
^
cc: Error: lab1.c, line 27: Invalid expression.
case default: dup2(f_des[0], fileno(stdin));
-----^
cc: Error: lab1.c, line 30: Invalid statement.
char *cmd[] = {"/usr/bin/"+argv[2], argv[2], (char *)0};
^
cc: Error: lab1.c, line 30: Missing ";".
char *cmd[] = {"/usr/bin/"+argv[2], argv[2], (char *)0};
------------------------------------------------------^
cc: Error: lab1.c, line 31: Invalid statement.
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
^
cc: Error: lab1.c, line 31: Missing ";".
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
------------------------------------------------------------^
cc: Error: lab1.c, line 32: Invalid statement.
int ret;
^
cc: Error: lab1.c, line 25: In this statement, "ret" is not declared.
ret = execvp(cmd,path);
^
cc: Error: lab1.c, line 25: In this statement, "cmd" is not declared.
ret = execvp(cmd,path);
-------------^
cc: Error: lab1.c, line 25: In this statement, "path" is not declared.
ret = execvp(cmd,path);
-----------------^

2 个答案:

答案 0 :(得分:2)

我尝试删除错误。我不确定这是否正常。

char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};

更改+ as,。

char *cmd[] = {"/usr/bin/",argv[1],argv[1], (char *)0};

case dafault:

default:

并将案例与块一样。使用参数。 { }

将参数传递给execvp的第一个参数是char *而不是char *[].

然后尝试此代码。我不确定这是否有效,但所有错误都已清除。

#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>

void main(int argc, char *argv[])
{
    int f_des[2];

   if(pipe(f_des) == -1)
    {
       perror("pipe");
       exit(1);
     }

     switch(fork())
     {
         case -1: perror("fork");
                 exit(2);
         case 0: { dup2(f_des[1], fileno(stdout));
                   close(f_des[0]);
                   close(f_des[1]);
                   char *cmd = "/usr/bin/";
                   char *const path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
                   int ret;
                   ret = execvp(cmd,path);
                   exit(3); }
         default: { dup2(f_des[0], fileno(stdin));
                   close(f_des[0]);
                   close(f_des[1]);
                   char *cmd = "/usr/bin/";
                   char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
                   int ret;
                   ret = execvp(cmd,path);
                   exit(4); }
        } 
  }  

答案 1 :(得分:1)

首先,要连接到C / C ++中的字符串,可以使用<string.h>&#39; s strcat(const char *,const char *)函数。其次,execvp()的第二个参数是const char *argv[]。第三,第一个参数是execvp()const char *cmd而不是char *cmd[],而是char *cmd。它必须是一个单一的字符串。此外,您输入的代码为default:而不是case default:。第四,您应该在切换声明之外声明retcmdpath / cmdargs

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

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

 if(pipe(f_des) == -1)
 {
     perror("pipe");
     exit(1);
 }

 char *cmd;
 char *cmdargs[/*<numcmdargs>*/];
 int ret;

 switch(fork())
 {
    case -1: perror("fork");
        exit(2);
    case 0: dup2(f_des[1], fileno(stdout));
        close(f_des[0]);
        close(f_des[1]);
        cmd = strcat("/usr/bin",argv[1]);
        cmd = strcat(cmd,'\0');
        ret = execvp(cmd,cmdargs);
        exit(3);
    default: dup2(f_des[0], fileno(stdin));
        close(f_des[0]);
        close(f_des[1]);
        cmd = strcat("/usr/bin/",argv[2]);
        cmd = strcat(cmd,'\0');
        ret = execvp(cmd,cmdargs);
        exit(4);
 }
}

注意:此代码将在clang上编译时没有警告,但可能需要进行一些修改才能运行。 (运行时它会给出Bus Error: 10