Assigning values via pointers in C/C++? (Visual Studio.NET 2003)

U

Usenet User

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!
 
G

Guest

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';
 
L

Larry Smith

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.

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!";
 
G

Guest

char *tmp[] = "Hello!"

should have been

char tmp[] = "Hello!"
--
John Hensley
www.resqware.com


John Hensley said:
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';

--
John Hensley
www.resqware.com


Usenet User said:
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!
 
G

Guest

The line:
char *tmp[] = "Hello!"
should be:
char tmp[] = "Hello!"

--
John Hensley
www.resqware.com


John Hensley said:
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';

--
John Hensley
www.resqware.com


Usenet User said:
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!
 
L

Larry Smith

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';
 
D

David Wilkinson

Usenet said:
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?

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!
 
U

Usenet User

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';

Doh!

Thanks , everyone! I suspected something like this was going on...

-UU
 

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

Top