473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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!
Nov 9 '07 #1
7 1273
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" wrote:
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!
Nov 9 '07 #2
>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!";
Nov 9 '07 #3
char *tmp[] = "Hello!"

should have been

char tmp[] = "Hello!"
--
John Hensley
www.resqware.com
"John Hensley" wrote:
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" wrote:
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!
Nov 9 '07 #4
The line:
char *tmp[] = "Hello!"
should be:
char tmp[] = "Hello!"

--
John Hensley
www.resqware.com
"John Hensley" wrote:
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" wrote:
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!
Nov 9 '07 #5
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';
Nov 9 '07 #6
Usenet User wrote:
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!

--
David Wilkinson
Visual C++ MVP
Nov 9 '07 #7
On Fri, 9 Nov 2007 16:31:40 -0500, "Larry Smith" <no_spam@_nospam.com>
wrote:
>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
Nov 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: angelag | last post by:
I am currently taking a college course in Visual Basic.Net and I am a beginner. I bought Visual Studio.Net 2003 to do my homework at home. I built my first project and e-mailed it to myself at...
6
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
0
by: Ricardo Dias Marques | last post by:
Hi, I have a development machine with Visual Studio .Net 2003 which, as far as I know, targets the 1.1 .Net Framework. Now I need to open some solutions that were created in another machine...
1
by: xman | last post by:
very hard is fix exactly group for this question.. but I am sure that some of people here have some experience with these compilers: Visual Studio NET Enterprise Architect 2002 and Visual Studio...
1
by: Daniel A. Thomas | last post by:
License required Maybe you don't have this but have one of the products that qualifies for the upgrade such as ... Visual Studio .NET 2003 Professional Visual Studio .NET 2003 Professional...
26
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio...
4
by: Dennis | last post by:
Where can I find the values for Windows Messages like MM_MCINOTIFY, MCI_NOTIFY_ABORTED, etc. I have looked and searched Google but can't seem to find a complete listing. -- Dennis in Houston
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.