1

Possible Duplicate:
tolower() not working

Here is my code:

char *ptr=&twod[j][i];

                 while (*ptr != '\0')
    {
        tolower(*ptr);


        cout<<endl
            <<endl
            <<endl
            <<*ptr;

        ptr++;

    }

When I cout the above, the the uppercase letters still remain as uppercase. Can someone explain to me why?

Community
  • 1
  • 1
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • `tolower` is a function from the C standard library. Remember that in C, `someFunction(variable)` can *never* modify the variable, because C has no notion of references. – fredoverflow Jan 27 '13 at 15:48

3 Answers3

6

You need to do this:

*ptr = tolower(*ptr);

std::tolower returns the lowercase. It does not accept the argument by reference, hence it cannot modify *ptr which you pass to it.

Read the documentation of std::tolower for detail.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • The argument to `tolower` must be cast to `unsigned char`. Read the documentation for detail. – Roland Illig Jan 03 '15 at 16:17
  • @RolandIllig: The caller doesn't need to cast. Just pass `*ptr` which is `char` which is fine if it is not negative. – Nawaz Jan 03 '15 at 16:33
  • "If it’s not negative." — But who guarantees you that? There are popular implementations out there where `char` can be negative. Since the question doesn’t give examples of particular strings, you have to expect _any_ character. – Roland Illig Jan 04 '15 at 21:59
2

tolower doesn't modify the value, it returns a new one.

Try this:

*ptr = tolower(*ptr);
Pubby
  • 51,882
  • 13
  • 139
  • 180
1

tolower doesn't mutate its argument. Set a variable or print out the operation:

*ptr = tolower(*ptr);
std::cout << *ptr;
// or
std::cout << tolower(*ptr);
David G
  • 94,763
  • 41
  • 167
  • 253