我对指针之类的东西相当陌生。该函数应该采用一个字符数组并将其拆分为组成单词,除非有引号(然后引号中的所有内容都保留在一起)。
在添加对引号的支持之前,它工作得很好,但现在我遇到了段错误。
int parse(char *line, char **args) {
int argc = 0;
while ( *line != '\0' ) {
if ( *line == '\'' || *line == '\"' ) {
*args++ = line;
++argc; ++line;
while ( *line != '\'' || *line != '\"' )
++line;
++line;
}
else {
while ( *line == ' ' || *line == '\t' || *line == '\n' )
*line++ = '\0';
*args++ = line;
++argc;
while ( *line != '\0' && *line != ' ' && *line != '\t' && *line != '\n' )
++line;
}
}
*--args = '\0';
return --argc;
}
最佳答案
您在 line
上的内部循环有缺陷,因为不检查字符串的结尾。这意味着如果字符串中没有结束引号,您将超出字符串的末尾。
关于c - 标记化 c block 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23639316/