I have a simple function which takes an array of characters as an argument, and converts all the characters to lower case. However, I get a weird access violation error. Here’s the code:
void toLower(char *rec)
{
int i=0;
while (rec[i]!='\0')
{
if (rec[i]>='A' && rec[i]<='Z')
rec[i]='a'+rec[i]-'A'; //this is where I get an error - assigning the
//the value to rec[i] is the problem
i++;
}
}
Can you tell me what’s my mistake?
Thanks
In a comment you say that you pass in a literal string to the function, like this:
where
palindromepasses its argument totoLower. This won’t work because string literals are read-only and you are trying to modify it. Instead, you can use:Or you can have
palindromecopy its argument into an array and calltoLoweron that.