Archived

This topic is now archived and is closed to further replies.

Terr

Differences between strings in VC5 and 6

Recommended Posts

Does anybody knows why this crashes my program in VC6? It worked in VC5. char *test3="This is test number 2"; test3[21]='3'; It's for replacing a character in a string. Edited by - Terr on February 21, 2002 8:03:27 PM

Share this post


Link to post
Share on other sites
Hmm, maybe because your changing the ''\0'' to ''3'', remember your index''s start at 0 not 1, so the last index is 20.

Share this post


Link to post
Share on other sites
Maybe i''ve miscount here, it''s a example, but even if I want to change the second letter or so, it crashes

Share this post


Link to post
Share on other sites
You''re editing a constant string in memory, in VC5 it must of not checked if it''s constant. If the change the code to for example

char SomeString[] = "Some text";

It should work, and here''s why, instead of allocating a static area of memory that can''t be edited then assign a pointer to it, the compiler tells the program to allocate an array like normal then fills it with the text given, which allows you to edit it.

Share this post


Link to post
Share on other sites
Well then id just say its another problem with the compiler then, but still if your going to use pointers allocate some memory before assigning them a value!

Share this post


Link to post
Share on other sites
thnx xDS4Lx and Omalacon, the char SomeString[] was the solution. And you''re right, I should allocate the memory first, but these are a just a few tests with string manipulation, memory management will come later

Share this post


Link to post
Share on other sites