Dear All,
Here is my code:
void main()
{
char *p="Hello";
*p='M'; //This is where the error occurs
cout<<p<<endl;
}
This code compiles and executes perfectly in Boreland C++. When I run the
same code in VC++ 6.0, however, I get the below error in debugging mode:
Unhandled exception in Test.exe: 0xC0000005: Access Violation
Can someone please provide me a solution to this?
Thanks in advance,
thejasviv 4 2492
thejasviv wrote:
Dear All,
Here is my code:
void main()
{
char *p="Hello";
*p='M'; //This is where the error occurs
cout<<p<<endl;
}
This code compiles and executes perfectly in Boreland C++. When I run the
same code in VC++ 6.0, however, I get the below error in debugging mode:
Unhandled exception in Test.exe: 0xC0000005: Access Violation
Can someone please provide me a solution to this?
thejasviv:
When you write
char *p = "Hello";
you should really write
const char *p = "Hello";
because the string literal "Hello" is (or at least may be) in read-only
memory. Omitting the const is only permitted for legacy C reasons. If
you want to modify the string you should write
char p[] = "Hello";
David Wilkinson
"David Wilkinson" <no******@effisols.comwrote in message
news:OX**************@TK2MSFTNGP02.phx.gbl...
thejasviv wrote:
>Dear All,
Here is my code:
void main() { char *p="Hello"; *p='M'; //This is where the error occurs cout<<p<<endl; }
This code compiles and executes perfectly in Boreland C++. When I run the same code in VC++ 6.0, however, I get the below error in debugging mode: Unhandled exception in Test.exe: 0xC0000005: Access Violation
Can someone please provide me a solution to this?
thejasviv:
When you write
char *p = "Hello";
you should really write
const char *p = "Hello";
because the string literal "Hello" is (or at least may be) in read-only
memory. Omitting the const is only permitted for legacy C reasons. If you
want to modify the string you should write
char p[] = "Hello";
Better, but still not correct. The compiler can still choose to use
read-only memory, because it's still a string literal.
Best is to use composite initializer syntax (an array is a composite):
char p[] = { "Hello" }; // identical to char[] p = { 'H', 'e', 'l', 'l',
'o', '\0' };
>
David Wilkinson
Dear David/Ben,
Thank you so much for this reply. The answer was so explanatory. All my
doubts got cleared. Here is the new code snippet which works perfectly well:
void main()
{
char p[]={"Hellow"};
*p='M';
cout<<p<<endl;
getch();
}
Thanks,
thejasviv
"Ben Voigt" wrote:
>
"David Wilkinson" <no******@effisols.comwrote in message
news:OX**************@TK2MSFTNGP02.phx.gbl...
thejasviv wrote:
Dear All,
Here is my code:
void main()
{
char *p="Hello";
*p='M'; //This is where the error occurs
cout<<p<<endl;
}
This code compiles and executes perfectly in Boreland C++. When I run the
same code in VC++ 6.0, however, I get the below error in debugging mode:
Unhandled exception in Test.exe: 0xC0000005: Access Violation
Can someone please provide me a solution to this?
thejasviv:
When you write
char *p = "Hello";
you should really write
const char *p = "Hello";
because the string literal "Hello" is (or at least may be) in read-only
memory. Omitting the const is only permitted for legacy C reasons. If you
want to modify the string you should write
char p[] = "Hello";
Better, but still not correct. The compiler can still choose to use
read-only memory, because it's still a string literal.
Best is to use composite initializer syntax (an array is a composite):
char p[] = { "Hello" }; // identical to char[] p = { 'H', 'e', 'l', 'l',
'o', '\0' };
David Wilkinson
On Mon, 1 Jan 2007 10:45:50 -0600, "Ben Voigt" <rb*@nospam.nospamwrote:
>char p[] = "Hello";
Better, but still not correct. The compiler can still choose to use read-only memory, because it's still a string literal.
But p is not a pointer; it's an array, and the compiler copies the string
literal into this non-const array. IOW, it's correct.
--
Doug Harrison
Visual C++ MVP This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daylor |
last post by:
hi.
im running an vb.net application exe that exist in my local network.
its run in computer A ,and exist in Computer B with other assemblies.
after an hour,(where the application ,dont do...
|
by: Kyle Teague |
last post by:
I have a list of pointers to structs as a private member of a class.
If I call begin() in the same function as I added the data then no
access violation occurs. However, if I try to call begin() in...
|
by: Microsoft News |
last post by:
I'm getting the following error when I shut down my C# .NET v1.1
application:
0xC0000005: Access violation reading location 0x73bc0000
This error didn't occur until I added a...
|
by: Bruce Pataki |
last post by:
I am creating an MFC application with activeX document server support. The
application runs perfectly fine when i run as a standalone application. But
when i run the application in Internet...
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
There is error message when executing my program,
Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access
violation reading location 0x00000002.
It is...
|
by: Dameon99 |
last post by:
Hi,
I have just spent many hours debugging my code and now when I run it I get the following error:
"An access violation (Segmentation fault) raised in your program"
I have researched on this...
|
by: nmehring |
last post by:
I have an MFC app with 2000 users. I have one user that experiences a
crash in our software anywhere from 1 to 5 times a week when opening a
particular module. No other users have reported this...
|
by: =?Utf-8?B?c29jYXRvYQ==?= |
last post by:
Hi,
I have a DLL in VC6, when a specific function is called it will spawns a
few threads and then return. The threads stay running and inside one of these
threads an event is created using the...
|
by: Martin |
last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP
5.2.5. I have not used or changed this site for several months - the
last time I worked with it, all was well.
When I tried it...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |