473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

toupper wont work

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
Jul 22 '05 #1
4 4127
"ben" <be*@ben.com> wrote in message
news:d%******************@twister.rdc-kc.rr.com
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #2
ben
thanks, it worked

"John Carson" <do***********@datafast.net.au> wrote in message
news:40******@usenet.per.paradox.net.au...
"ben" <be*@ben.com> wrote in message
news:d%******************@twister.rdc-kc.rr.com
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3
"ben" <be*@ben.com> writes:
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[i] = std::toupper(t[i]);
std::cout << t << std::endl;
return 0;
}

HTH & kind regards
frank

--
Frank Schmitt
quattro research GmbH
e-mail: schmitt NO at SPAM quattro-research !@! dot com
Jul 22 '05 #4
Pete Becker <pe********@acm.org> wrote:
Frank Schmitt wrote:
char *t="test";
printf(t);


This is asking for trouble - use
print("%s",t);


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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Duane | last post by:
Aside from the pitfalls of using this function, according to the standard, what is the correct way to call it? #include <string> #include <locale> // seems to work with BCC5.6/STLPort...
18
by: didgerman | last post by:
Chaps, I need to properly format the case of a struct. Can I just hit it with tolower, and then 'while (string ==' ') pos++; string=toupper(string); to add in the higher case for the start of...
0
by: Shrinivas Reddy | last post by:
Hi, I am using the ToUpper() function in an FXCop rule which checks whether a boolean variable has "is" or "has" as the prefix. The ToUpper() function does not work. When I put the expression...
48
by: Frederick Gotham | last post by:
The "toupper" function takes an int as an argument. That's not too irrational given that a character literal is of type "int" in C. (Although why it isn't of type "char" escapes me... ) The...
4
by: sandy | last post by:
I am trying to upper case a string, so I have this method: string FileSystem::toupper(string S) { for (int i=0; i<S.length(); ++i) { S=toupper(S); } return S;
13
by: JanWhitney | last post by:
I am learning C++, so please be kind. Is there anywhere that I can view the source code for the toUpper function? Thanks.
16
by: gaga | last post by:
my function should accept a pointer to a string as its argument. and it should convert each charater to an uppercase letter. I know what i need to do, its just my syntax is all out of whack. ...
2
by: Thomas | last post by:
I have the following managed code: String* ts; ts = S" test "; ts = ts->Trim(); ts = ts->ToUpper(); The ToUpper does not work; at the end of this ts is still lower case. What is the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.