This site is no longer active and is available for archival purposes only. Registration and login is disabled.

This should work right??!?


This should work right??!?

Postby RwGast » Jul 27, 2001 @ 3:34pm

     <br>char* Test = "Test";<br>*(Test) = 'w';<br><br>Should make the T = w right?! well this code crashes dos and the debugger says that its the <br>*(Test) = 'w'; line. Is there somthing i am doing wrong? this technique works fine if i use an array like char Test[]="Test"; but not when i use a pointer.
http://www.angelfire.com/ego/esoteric if you like to play quake3 keep your eye on this site
User avatar
RwGast
pm Member
 
Posts: 1123
Joined: Jun 28, 2001 @ 7:36pm
Location: California, USA


Re: This should work right??!?

Postby Dan East » Jul 27, 2001 @ 4:21pm

First off, are you building for Windows CE or for a desktop machine? If the former, then you should be using Unicode. Second, there is a big difference between:<br><br>[fixed]char *Test="Test";[/fixed]<br><br>and<br><br>[fixed]char Test[]="Test";[/fixed];<br><br>In the first you are creating a pointer called Test. What does it point to? The constant string "Test". You are not supposed to modify const values, hence it may crash.<br>In the second example you create an array of characters, and the compiler automatically makes it large enough to hold the string you want to initialize it with. So when you modify that first character, you are simply modifying the first value in the array you created.<br>Make sense?<br><br>Dan East
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: This should work right??!?

Postby RwGast » Jul 27, 2001 @ 4:50pm

how come the value of *Test is constant i did not define it as const char* Test = "Test"; also in the prevous thread of is this bad code? Fredrick used an example of changing the value of a string declared like that. Thats what made me think it was possible.
http://www.angelfire.com/ego/esoteric if you like to play quake3 keep your eye on this site
User avatar
RwGast
pm Member
 
Posts: 1123
Joined: Jun 28, 2001 @ 7:36pm
Location: California, USA


Re: This should work right??!?

Postby Digby » Jul 27, 2001 @ 8:59pm

From the ANSI ISO C++ spec, <br><br>2.13.4 String literals<br><br>2. Whether all string literals are distinct (that is, stored in nonoverlapping objects) is implementation defined. The effect of attempting to modify a string literal is undefined.<br><br>Looks like in your situation "undefined" means access violation because the string literal is probably stored in a read-only protected section of memory.<br><br><br><br><br>
Digby
pm Insider
 
Posts: 1011
Joined: Apr 29, 2001 @ 1:53pm


Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron