如何将输入响应传递给C中的函数

时间:2014-05-06 16:35:14

标签: c arrays pointers

我试图询问用户他们想要在" shell"我做过的。我的代码如下:

    /* Create a char array to hold response */
char response[80];
char exit[4] = "Exit";

printf("\n\n     Commands are as followed:");
printf("\n------------------------------------");
printf("\n'cd' 'Directory' (Changes Directorys)");
printf("\n'cp' 'FileName'  (Copys File)");
printf("\n'Exit'           (Exits the program)");
printf("\n'ls'             (Displays Info)");
printf("\n------------------------------------\n\n");

while(strcmp(exit, response) != 0){

/* Ask user for input */
fputs("$> ", stdout);

/* Flush */
fflush(stdout);

/* Make sure response is not NULL */
if ( fgets(response, sizeof response, stdin) != NULL )
{
    /* search for newline character */
    char *newline = strchr(response, '\n'); 
    if ( newline != NULL )
    {
        /* overwrite trailing newline */
        *newline = '\0'; 
    }

    actOnResponse(fs, response); 
 }

现在我的问题涉及actOnResponse()函数。我想将响应传递给函数。在函数中,我将解析响应,并将字符串与值' cd',' cp'进行比较。和' ls'。但是我该怎么做呢?现在,当我运行程序时,它给了我一个分段错误(Core Dump)。

任何可以指出我做错的人都会很棒!谢谢。

这是actOnResponse()函数:

void actOnResponse(int fs, char *response){

printf("%s" , response);
}

3 个答案:

答案 0 :(得分:2)

  1. 初始化响应,例如响应[0] = 0。
  2. 退出长度为5(结束零)。使用char exit [] ="退出&#34 ;;否则你的strncmp可能会失败。
  3. fs未定义。
  4. 此代码有效:

    #include <stdio.h>
    #include <string.h>
    
    void actOnResponse(int fs, char *response){
      printf("%s\n" , response);
    }
    
    void main() {
      /* Create a char array to hold response */
      char response[80];
      char exit[] = "Exit";
      int fs = 0;
    
      response[0] = 0;
    
      printf("\n\n     Commands are as followed:");
      printf("\n------------------------------------");
      printf("\n'cd' 'Directory' (Changes Directorys)");
      printf("\n'cp' 'FileName'  (Copys File)");
      printf("\n'Exit'           (Exits the program)");
      printf("\n'ls'             (Displays Info)");
      printf("\n------------------------------------\n\n");
    
      while (strcmp(exit, response) != 0) {
        /* Ask user for input */
        fputs("$> ", stdout);
    
        /* Flush */
        fflush(stdout);
    
        /* Make sure response is not NULL */
        if (fgets(response, sizeof response, stdin) != NULL) {
          /* search for newline character */
          char *newline = strchr(response, '\n');
          if (newline != NULL) {
            /* overwrite trailing newline */
            *newline = '\0';
          }
          actOnResponse(fs, response);
        }
      }
    }
    

    (保存为test.c。运行gcc -o test test.o然后./test)。

答案 1 :(得分:1)

这里exit [4]不包含字符串结束字符(因为它不是exit [5])而且response [80]也没有初始化,因此它也不包含字符串ecd charecter。

因此当它与 strcmp 一起使用时,它无法找到字符串的结尾并且内存不足则会出现此分段错误。

所以这是由于这个变量的组合。

答案 2 :(得分:0)

阅读 中的注释,原因可能是seg错误。

以下代码有效:(添加了遗失}while声明配对)
通过以下小编辑,我从未见过seg错误。

void actOnResponse(int fs, char *response);


int main(void)
{
    char response[80]={0};
    //char exit[>>>4<<<] = "Exit";  //seg fault may have happened resulting from this line, 
                              //need space for 5, including `\0`  Otherwise, `exit` would not
                              //be legal string, potentially cause string functions such as
                              // strcmp() or stricmp() etc. to seg fault.

    char exitt[] = "Exit";  //conflict with my system, changed `Exit` to `Exitt` throughout

    printf("\n\n     Commands are as followed:");
    printf("\n------------------------------------");
    printf("\n'cd' 'Directory' (Changes Directorys)");
    printf("\n'cp' 'FileName'  (Copys File)");
    printf("\n'Exit'           (Exits the program)");
    printf("\n'ls'             (Displays Info)");
    printf("\n------------------------------------\n\n");

    while(strcmp(exitt, response) != 0){

    /* Ask user for input */
    fputs("$> ", stdout);

    /* Flush */
    fflush(stdout);

    /* Make sure response is not NULL */
    if ( fgets(response, sizeof response, stdin) != NULL )
    {
        /* search for newline character */
        char *newline = strchr(response, '\n'); 
        if ( newline != NULL )
        {
            /* overwrite trailing newline */
            *newline = '\0'; 
        }

        actOnResponse(1, response); //not sure what 1st argument is supposed to be, just used `1`
     }
    }  //This was missing 
    return 0;
}


void actOnResponse(int fs, char *response)
{
    printf("%s" , response);
}