Reputation: 1
I'm trying to write a code that reads from a file and works with the characters it reads. The gist is that it has to correct the capitalization errors present in the file it reads.
One particular requirement is that I have to number each line, so I wrote a bit to determine whether or not a each character read is a line break.
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++;
}
However, the function this is in always returns 0, when it should return 1 at the end of each line. What am I doing wrong?
Upvotes: 0
Views: 110
Reputation: 16540
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.
Upvotes: 1