views:

164

answers:

4

Hi,

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

+6  A: 

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);
}
Billy ONeal
True, but won't solve the problem here (which is probably a bad argument being passed in).
interjay
@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
Thanks, but the point is that I want to exercise my C a little :)
misaizdaleka
@misaizdaleka: Then you should tag your question C, not C++.
Billy ONeal
@ misaizdalekaExercise your C skills by doing something novel :) Why reinvent the wheel?
Truncheon
Because this is what they ask on a job interview! :)
misaizdaleka
Your post doesn't answer OP's question
qrdl
@qrdl: Yes, but the question has been edited since I posted this answer.
Billy ONeal
+1  A: 

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

Kyle Alons
+6  A: 

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.

Andrew Aylett
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
Looks like this is a problem -- trying to modify a string literal.
Fred Larson
+6  A: 

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";
palindrome(str);

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

interjay
Thanks! Solved it! However, before trying char str[] = "In girum imus nocte et consumimur igni";I triedchar *str = "In girum imus nocte et consumimur igni";And it didn't work. Why? What's the difference?
misaizdaleka
`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
Great! Thank you very much for your time!
misaizdaleka