我正在试着写一段代码,它能从文件中读取数据,并能处理所读取的字符。要点是它必须纠正它读取的文件中存在的大小写错误。
一个特殊的要求是我必须对每一行进行编号,所以我编写了一些代码来确定读取的每个字符是否是换行符。
int fix_caps(char* ch, int* char_in_word, int* line_num){
char a;
ch = &a;
if(a != '\n'){
return 0;
}else{
return 1;
}
if(a == ' ')
*char_in_word = 0;
if(*char_in_word == 1)
a = toupper(a);
if(*char_in_word > 1)
a = tolower(a);
char_in_word++;
}然而,this所在的函数总是返回0,而它应该在每行的末尾返回1。我做错了什么?
发布于 2015-03-22 08:54:10
the execution will never get beyond this 'if control block:
char a;
ch = &a;
if(a != '\n'){
return 0;
}else{
return 1;
}
there is a few reasons it 'always' returns 0
1) 'a' is on the stack and could contain anything.
2) the chances of the 'trash' that is on the stack where 'a'
is located are 255:1 against the trash happening to contain
a new line character.
nothing beyond the 'if control block is ever executed because
an 'if' control block only has two execution paths
and both paths contain a return statement.https://stackoverflow.com/questions/29189766
复制相似问题