473,797 Members | 3,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1292
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@_nospa m.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
7270
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 school. When I tried to open it in the lab, I got a message saying I couldn't open it because it was created with a newer version. Evidently the lab is using Visual Studio.Net 2002. My professor doesn't just want the executable file, he wants...
6
6178
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 itself. Everything you need is available at no costs (except download hassle and installation time). Once your system is set up properly its just a matter of running 'python setup.py build'. No longer waiting for someone else to build binaries and a...
0
6144
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 file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL" /D "_AFXDLL" /D "_MBCS" /D "_USRDLL" /
0
1868
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 with Visual Studio .Net 2002 (against the 1.0 Framework). I have already read:
1
2531
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 NET 2003 1. What is difference between Visual Studio NET Enterprise Architect 2002 and Visual Studio NET 2003 2. If I try user Visual Studio NET 2003, what I have not included inside (in compare with Visual Studio NET 2003)?
1
2545
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 Special Edition Visual Studio .NET 2003 Enterprise Architect Visual Studio .NET 2003 Enterprise Developer Visual Studio .NET 2002 Professional Edition
26
10885
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 compiles the same solution much faster (about 10 seconds for the first project). My home computer is only marginally faster than the one I have at the office (P4 2.53 vs. P4 2.4, same amount of RAM). On the slow machine, the CPU usage is very low,...
4
1769
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
2556
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 reference: //Managed c++ QueryResult* qr = NULL; ExecuteQuery(1, "abc", qr); //invoke native c++ method passing qr by reference
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.