toupper wont work

B

ben

Hi

Im gettting run time error (Unhandled exeption, access violations) when
executing the following code:

#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string>
#include <stdlib.h>

int main()
{

char *t="test";

printf(t);

*t = toupper(*t); //error here

printf(t);

getch();
return 0;

}
I running it on VC++ 6

All I want to do is to convert a string to upper case. Am I missing
something here?

Thanks
 
J

John Carson

ben said:
Hi

Im gettting run time error (Unhandled exeption, access violations)
when executing the following code:

#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string>
#include <stdlib.h>

int main()
{

char *t="test";

printf(t);

*t = toupper(*t); //error here

printf(t);

getch();
return 0;

}
I running it on VC++ 6

All I want to do is to convert a string to upper case. Am I missing
something here?

Thanks

toupper doesn't convert strings, it converts characters. As it happens, this
is not your problem. Your problem is that

char *t="test";

creates a pointer to a string literal that is stored in read-only memory.
Thus you cannot change it. What you need is:

char t[]="test";

which creates a writeable array.
 
B

ben

thanks, it worked

John Carson said:
ben said:
Hi

Im gettting run time error (Unhandled exeption, access violations)
when executing the following code:

#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string>
#include <stdlib.h>

int main()
{

char *t="test";

printf(t);

*t = toupper(*t); //error here

printf(t);

getch();
return 0;

}
I running it on VC++ 6

All I want to do is to convert a string to upper case. Am I missing
something here?

Thanks

toupper doesn't convert strings, it converts characters. As it happens, this
is not your problem. Your problem is that

char *t="test";

creates a pointer to a string literal that is stored in read-only memory.
Thus you cannot change it. What you need is:

char t[]="test";

which creates a writeable array.
 
F

Frank Schmitt

ben said:
Hi

Im gettting run time error (Unhandled exeption, access violations) when
executing the following code:

#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string>
#include <stdlib.h>

int main()
{

char *t="test";

printf(t);

This is asking for trouble - use
print("%s",t);
*t = toupper(*t); //error here

printf(t);

getch();
return 0;

Better yet, get rid of C-style strings altogether and use C++ strings
and I/O:

#include <cctype>
#include <string>
#include <iostream>

int main() {

std::string t = "test";
std::cout << t << "\n";
for (std::size_t i=0; i<t.length(); ++i)
t = std::toupper(t);
std::cout << t << std::endl;
return 0;
}

HTH & kind regards
frank
 
D

Dietmar Kuehl

Pete Becker said:
There's nothing wrong wtih printf(t).

Well, in this case there is nothing wrong with 'printf(t)'. If 't' is obtained
from somewhere, eg. from user input, it is not really a bright idea to use
't' as format string, however: it may contain formatting directives by
accident in which case the call to 'printf(t)' might easily yield funny
behavior.

Of course, this is not really related to the original problem which was
writing to string literal. The other problem not yet noted in this thread
was the fact that 'char' may be signed but 'toupper()' has to be called with
an 'unsigned char' or EOF (this is described in 7.4 paragraph 1 of the current
C standard; I don't have the C90 standard but I'm sure the same rule applies
there, too). Thus, the program should look something like this:

char t[] = "test";
for (char* it = t; *it; ++it)
*it = std::toupper(static_cast<unsigned char>(*it));

Of course, for "test" it is again unlikely to make any difference because
normally these characters happen to be represented by ASCII which uses all
positive values. However, in a real application the characters may include
non-ASCII character which may have negative values. Of course, in this case
there may be other problems as well: for example, the string may include the
German "sz" ligature for which there is no single character uppercase variant.
The uppercase variante of the "sz" ligature (which is just one character) has
two characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top