So I’m not very good with C but I’m designing a GLUT application that reads in a file that is not case sensitive. To make it easier I want to convert my strings to all lower case. I made a function makeLower that is modifying a variable that was passed in by reference.
I have a While Loop in the method makeLower that seems to get through part of the first iteration of the while loop and then the EXE crashes. Any tips would be great, thanks!
Output:
C:\Users\Mark\Documents\Visual Studio 2010\Projects\Project 1\Debug>"Project 1.e
xe" ez.txt
Line is #draw a diamond ring
Character is #
Then error “project 1.exe has stopped working”
Code:
void makeLower(char *input[]){
int i = 0;
printf("Line is %s\n", *input);
while(input[i] != "\0"){
printf("Character is %c\n", *input[i]);
if(*input[i] >= 'A' && *input[i] <= 'Z'){
*input[i] = tolower(*input[i]);
}
i++;
}
}
int main(int argc, char *argv[]) {
FILE *file = fopen(argv[1], "r");
char linebyline [50], *lineStr = linebyline;
char test;
glutInit(&argc, argv);
while(!feof(file) && file != NULL){
fgets(lineStr , 100, file);
makeLower(&lineStr);
printf("%s",lineStr);
//directFile();
}
fclose(file);
glutMainLoop();
}
I see more problems now, so I extend my comments to an answer:
You allocate an array of 50 characters, but tell
fgetsto get up to 100 characters, which might be fatal asfgetswill overwrite memory not in the string.When passing a C string to a function, you don’t have to pass the address of the pointer to the string (
&lineStr), the actual pointer or array is okay. This means you can change themakeLowerfunction tovoid makeLower(char *input)orvoid makeLower(char input[]). Right now the argument tomakeLoweris declared as an array or char pointers, not a pointer to an array of char.In the new
makeLowerI proposed above, you can access single characters either as an array (input[i]) or as a pointer plus offset (*(input + i). Like I said in my comment, the last version is what the compiler will probably create if you use the first. But the first is more readable so I suggest that.Also in
makeLoweryou make a comparison with"\0", which is a string and not a character. This is almost right actually: You should useinput[i] != '\0'.And finally this is how I would implement it:
A few explanations about the function:
strlenorstrcpy.)*inputdereferences (i.e. takes the value of what a pointer points to) the string. It is the same as*(input + 0)and so get the value of the first character in the string.'\0'(which technically is a normal zero) we will loop.tolowerfunction. This will work no matter what the character is,tolowerwill only turn upper case characters to lower case, all other characters will be returned as they already were.tolowercopied over the first character. This works because the right hand side of an assignment must be executed before the assignment, so there will not be any error or problem.inputpoint to the next character in the string. This works becauseinputis a local variable, so operations on the pointer will not affect anything in the calling function.This function can now be called like this: