Access Violation error while using pointers

G

Guest

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
 
D

David Wilkinson

thejasviv said:
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
 
B

Ben Voigt

David Wilkinson said:
thejasviv said:
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' };
 
G

Guest

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 said:
David Wilkinson said:
thejasviv said:
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
 
D

Doug Harrison [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top