Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Usenet User - 09 Nov 2007 20:32 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:

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.