我试图编写一个从文件中读取并使用它读取的字符的代码。要点是它必须纠正它所读取的文件中存在的大写错误。
一个特殊要求是我必须为每一行编号,所以我写了一点来确定每个字符读取是否为换行符。
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++;
}
然而,当它应该在每行的末尾返回1时,它所在的函数总是返回0。我做错了什么?
答案 0 :(得分:1)
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.