By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

Access Violation error while using pointers

P: n/a
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
Dec 31 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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


Jan 1 '07 #2

P: n/a

"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


Jan 1 '07 #3

P: n/a
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




Jan 1 '07 #4

P: n/a
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
Jan 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.