我正在尝试用 C 编写一个类似于命令提示符的函数。我收到一个段错误。我的逻辑如下: getchar() 并检查它是否为空格。如果不是空格,则将字符添加到 msg 中。如果是空格,则结束单词并将其添加到命令数组中,然后获取下一个单词。当它得到新行时,它就结束了。
void getCommand(char *command[10]){
char *msg = ""; //Creates ptr
char c;
int length = 0;
while(getchar() != '\n'){ //while char != new line
c = getchar(); // get char
if(c == ' '){ //if char equal space
c = '\0'; //make c equal end of line
msg[length] = c; //add c to the end of line
strcpy(*command, msg); //copy the word into the first part of the array
command++; //increase command array
}
else{ //if word does not equal space
msg[length] = c; //add char to the msg
length++; //increase length by one
}
}
}