我正在处理一个函数,该函数接受一个字符串参数(实际上从主函数发送一个char数组,但不要认为这有所不同)在C中,并解析要存储在链接中的字符串列表(这是学校的作业),我收到了分段错误。我已经在整个程序中添加了要打印的字符串以调试程序,因为它依赖于输入文件,我不知道如何使用输入选项启动gdb。
这就是我的功能:
Token * list_head = NULL;
Token * current = NULL;
char line_segment[MAX_PRINT_LINE_LENGTH + 1];
int i, pos = 0; //In the for loop, we do not not to lose the pos in the array
puts("in token.c, beginning segmentation");
/***************************************************************************
* Loop through the possibilites of the first character starting at the
* position located at the pointer (pretty sure this is still easier on the
* memory and logically then enumerating all of the different symbols and
* types of chars that you will see.
**************************************************************************/
for(i=0; line_copy[pos] != '\0';){
if(line_copy[pos] == 'a' || 'b' || 'c' || 'd' || 'e' || 'f' || 'g' ||
'h' || 'i' || 'j' || 'k' || 'l' || 'm' || 'n' ||
'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' ||
'v' || 'w' || 'x' || 'y' || 'z' || 'A' || 'B' ||
'C' || 'D' || 'E' || 'F' || 'G' || 'H' || 'I' ||
'J' || 'K' || 'L' || 'M' || 'N' || 'O' || 'P' ||
'Q' || 'R' || 'S' || 'T' || 'U' || 'V' || 'W' ||
'X' || 'Y' || 'Z') //Identifier or Rw
do{
line_segment[i] = line_copy[pos];
pos++; i++;
} while(line_copy[pos] == //All letters of alphabet);
else if(line_copy[pos] == '0' || '1' || '2' || '3' || '4' || '5' ||
'6' || '7' || '8' || '9'){ //Number
do{
line_segment[i] = line_copy[pos];
pos++; i++;
} while(line_copy[pos] == '0' || '1' || '2' || '3' || '4' || '5' ||
'6' || '7' || '8' || '9' || 'e');
}
else if(line_copy[pos] == " "){ //No token
line_segment[i] = '\0';
pos++;
}
else if(line_copy[pos] == "'"){ //String
do{ //We want to keep the first apostrophe for make_node
line_segment[i] = line_copy[pos];
pos++; i++;
} while(line_copy[pos] != "'");
pos++; //Don't want to start on the ending ' symbol.
}
else{ //Symbol
line_segment[i] = line_copy[pos];
pos++;
}
puts("line segmented, attempting node");
printf("line segment: %s\n", line_segment);
current = make_node(line_segment);
memset(line_segment, '\0', MAX_PRINT_LINE_LENGTH);
puts("attempting to add node to list");
list_head = add_to_list(list_head, current);
free(current);
puts("node freed");
}
return list_head;
}
答案 0 :(得分:2)
if(line_copy[pos] == 'a' || 'b' || 'c' || 'd' || 'e' || 'f' || 'g' ||
'h' || 'i' || 'j' || 'k' || 'l' || 'm' || 'n' ||
'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' ||
'v' || 'w' || 'x' || 'y' || 'z' || 'A' || 'B' ||
'C' || 'D' || 'E' || 'F' || 'G' || 'H' || 'I' ||
'J' || 'K' || 'L' || 'M' || 'N' || 'O' || 'P' ||
'Q' || 'R' || 'S' || 'T' || 'U' || 'V' || 'W' ||
'X' || 'Y' || 'Z')
您必须遵循实际的C语法。你无法做到并期望它们发挥作用。你正在对一堆字母进行ORing,这没有任何意义。
答案 1 :(得分:0)