Take the 2-minute tour ×

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

Can you tell me what's my mistake? Thanks

share|improve this question
How do you call toLower? –  KennyTM Apr 14 '10 at 14:10
If this isn't homework, there is a C++ standard library function, std::tolower in <cctype> that does just this. –  James McNellis Apr 14 '10 at 14:11
@James McNellis: But tolower only works on a single character. –  Billy ONeal Apr 14 '10 at 14:12
Is this array of characters or string? If this is array, you need array length as additional parameter. –  Alex Farber Apr 14 '10 at 14:14
No, this is not a homework, I'm just exercising my C++ which got rusty a bit... :) @Kenny: I use this: toLower(rec); where rec is also passed to a function from which I call toLower like this: int palindrome(char *rec) { ... @Alex: this is an array, but in the end I have a null terminating string, so it's not a problem. And btw, the function crashes in its first iteration. –  misaizdaleka Apr 14 '10 at 14:22
add comment

4 Answers

up vote 6 down vote accepted

In a comment you say that you pass in a literal string to the function, like this:

palindrome("In girum imus nocte et consumimur igni")

where palindrome passes its argument to toLower. This won't work because string literals are read-only and you are trying to modify it. Instead, you can use:

char str[] = "In girum imus nocte et consumimur igni";

Or you can have palindrome copy its argument into an array and call toLower on that.

share|improve this answer
Thanks! Solved it! However, before trying char str[] = "In girum imus nocte et consumimur igni"; I tried char *str = "In girum imus nocte et consumimur igni"; And it didn't work. Why? What's the difference? –  misaizdaleka Apr 14 '10 at 14:40
char *str ="..." is a pointer that receives the address of the read-only string literal, which can't be modified. char str[]="..." is an array, so it can be modified. –  interjay Apr 14 '10 at 14:44
Great! Thank you very much for your time! –  misaizdaleka Apr 14 '10 at 14:46
add comment

You're working too hard :)

#include <algorithm> //For std::transform
#include <cctype> //For std::tolower
#include <cstring> //For std::strlen

void toLower(char *rec)
    std::transform(rec, rec + std::strlen(rec), rec, std::tolower);
share|improve this answer
True, but won't solve the problem here (which is probably a bad argument being passed in). –  interjay Apr 14 '10 at 14:18
@interjay: Yes, but if he does this he can eliminate this function as the likely cause of the problem. Standard library functions don't make mistakes. –  Billy ONeal Apr 14 '10 at 14:19
Thanks, but the point is that I want to exercise my C a little :) –  misaizdaleka Apr 14 '10 at 14:27
@misaizdaleka: Then you should tag your question C, not C++. –  Billy ONeal Apr 14 '10 at 14:30
@ misaizdaleka Exercise your C skills by doing something novel :) Why reinvent the wheel? –  Truncheon Apr 14 '10 at 14:34
show 3 more comments

Are you passing in (even indirectly) a string literal? If so, then it may be loaded in unwriteable memory; you'll need to make changes on a copy.

That your function prototype takes a char * rather than a const char * suggests that you've probably not done this, but I thought I`d throw it out.

share|improve this answer
OK, in the main program I do this: printf ("%d \n", palindrome("In girum imus nocte et consumimur igni")); then, palindrome(char *rec) calls toLower(char *rec), passing the string which I passed in the first place. –  misaizdaleka Apr 14 '10 at 14:25
Looks like this is a problem -- trying to modify a string literal. –  Fred Larson Apr 14 '10 at 14:32
add comment

Sounds like you didn't pass in a valid buffer. But don't write this yourself; use something like strlwr.

share|improve this answer
add comment

Your Answer


