我正在尝试编写一个一次解析字符串(a char *)字符的函数,但由于某种原因,我收到了分段错误。我正在尝试读取用户输入并将其解析为程序名称和参数,但这有点不敬。
安慰:
>./mish 
>mish>ls
>command start:ls 
>*tempCommand:l 
>bottom
>Segmentation fault
代码:
    ParserData parseCommand(ParserData parserData, char* command)
    {
        char* tempCommand;
        char* currToken = '\0';
        short firstSpace = 0;
        printf("command start:%s \n", command);
        strcpy(tempCommand, command);
        while(*tempCommand)
        {
            printf("*tempCommand:%c \n", *tempCommand);
            if((char)*tempCommand == ' ' && firstSpace == 0)
            {
               printf("currToken: %c \n", (char)*currToken);
               strcpy(parserData.myChildProgramName,currToken);
               printf("after:");
               currToken = '\0';
               firstSpace = 1;
            }
            else if((char)*tempCommand != ' ' && *tempCommand != '-')
            {
              //strcat(currToken, tempCommand); 
            }
            printf("bottom\n");
            printf("currToken: %c \n", *currToken);
            tempCommand++;
       }
       printf("out\n");
       parserData.myChildProgramArguments = currToken;
      return parserData;
   }