我有一个字符指针,用于CSV文件的部分文件名。 我需要让它遵循以下模式:0, 1, 2, ..., 9, A, B, ..., Y, Z, 10, 11, ...等等永远。以下代码是处理程序中处理该问题的部分,但是当我尝试增加字符指针FILE_num
中的一个"数字"时,我遇到了分割错误。我很确定我现在那里的内容将指针递增到下一个地址(不存在)。
我尝试了许多不起作用的东西(其中一半做同样的事情,另一半不编译)。这可能是其他失败的东西,但我相当确定就是这样。不幸的是,我必须使用 C,所以我不能使用字符串。
char * FILE_num ;
//
// from earlier in the program
//
FILE_num = (char *) malloc (sizeof (char)) ;
FILE_num = "0" ;
//
// skip several things that work but do not relate (maybe)
//
int lines = 0 ,
digit ;
//
// some code that counts lines in a file
//
if (lines > 0xfffff) { // if file is too large start new file
for (digit = strlen (FILE_num) ; digit >= 0 ; digit --) {
if (FILE_num [digit] == 'Z') { // add to proper digit
FILE_num [digit] = '0' ;
}
else if (FILE_num [digit] == '9') { // skip :;<=>?@
FILE_num [digit] = 'A' ;
break ;
}
else {
FILE_num [digit] ++ ; // segmentation fault here
break ;
}
}
//
// Some other code to handle overflow of digits
//
}
您有内存泄漏和未定义的行为。
FILE_num = "0" ;
导致内存泄漏 - 丢失了动态分配的内存的地址。
"0"是一个包含字符'0'
和