I am using Visual Studio.NET 2003, and to my surprise, the below code
in a simple console application does not work for me:
char *str = "hello!";
*str = 'H'; <-- error
str[0] = 'H'; <-- error
The above code compiles just fine, but I am getting runtime errors.
If managed extensions are used, I'm getting "Objest reference is not
set to an object." If I disable managed extensions, I get "Access
violation" errors.
Any suggestions?
TIA!
John Hensley - 09 Nov 2007 21:18 GMT
The string "Hello!" is a const value that is located in a read-only area of
memory. When you try to change the first character of the string with *str =
'H' and str[0]=h you are attempting to overwrite read-only memory and that is
why you are getting an exception.
Use something like this if you want to be able to overwrite your string.
char *tmp[] = "Hello!"
char *str = tmp;
*str = 'H';
str[0] = 'H';

Signature
John Hensley
www.resqware.com
> I am using Visual Studio.NET 2003, and to my surprise, the below code
> in a simple console application does not work for me:
[quoted text clipped - 11 lines]
>
> TIA!
John Hensley - 09 Nov 2007 21:25 GMT
char *tmp[] = "Hello!"
should have been
char tmp[] = "Hello!"

Signature
John Hensley
www.resqware.com
> The string "Hello!" is a const value that is located in a read-only area of
> memory. When you try to change the first character of the string with *str =
[quoted text clipped - 23 lines]
> >
> > TIA!
Larry Smith - 09 Nov 2007 21:31 GMT
> char *tmp[] = "Hello!"
>
> should have been
>
> char tmp[] = "Hello!"
You don't need "tmp". This will do fine:
char str[] = "Hello!";
*str = 'H';
str[0] = 'H';
Usenet User - 09 Nov 2007 23:57 GMT
>> char *tmp[] = "Hello!"
>>
[quoted text clipped - 7 lines]
>*str = 'H';
>str[0] = 'H';
Doh!
Thanks , everyone! I suspected something like this was going on...
-UU
John Hensley - 09 Nov 2007 21:27 GMT
The line:
char *tmp[] = "Hello!"
should be:
char tmp[] = "Hello!"

Signature
John Hensley
www.resqware.com
> The string "Hello!" is a const value that is located in a read-only area of
> memory. When you try to change the first character of the string with *str =
[quoted text clipped - 23 lines]
> >
> > TIA!
Larry Smith - 09 Nov 2007 21:19 GMT
>I am using Visual Studio.NET 2003, and to my surprise, the below code
> in a simple console application does not work for me:
[quoted text clipped - 7 lines]
> set to an object." If I disable managed extensions, I get "Access
> violation" errors.
You can't assign to a string literal. It's type is "const char[]". Your
declaration should therefore be:
const char *str = "hello!";
The "const" can be omitted for backwards compatibility with C but it's still
illegal to write to this space. You should therefore specify the "const". If
you wnat to modify it then copy it instead:
char str[] = "hello!";
David Wilkinson - 09 Nov 2007 22:06 GMT
> I am using Visual Studio.NET 2003, and to my surprise, the below code
> in a simple console application does not work for me:
[quoted text clipped - 9 lines]
>
> Any suggestions?
UU:
When you write
char *str = "hello!";
the string is (or at least may be) in read-only memory. That is why you
get a runtime error when you try to change it. In fact in C++ you should
write
const char *str = "hello!";
and then
*str = 'H';
will fail to compile. The form
char *str = "hello!";
is only allowed to provide compatibility with legacy C code. Don't do it!

Signature
David Wilkinson
Visual C++ MVP