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

exception : 0xC0000005: Access Violation

P: n/a
Hi,

I am getting the following exception

First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation

When i run the following code

1 int main(void)
2 {
3 char* str1 = "MA";
4 char s = str1[0];
5 str1[0] = 'r';
6 return 0;
7 }

i'm getting exception on line 5. Why?

Bye.

Nov 7 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

batista wrote:
Hi,

I am getting the following exception

First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation

When i run the following code

1 int main(void)
2 {
3 char* str1 = "MA";
4 char s = str1[0];
5 str1[0] = 'r';
6 return 0;
7 }

i'm getting exception on line 5. Why?


str is a constant string , you can only access it. If you want to
change the contents of str, you will either have to declare it as an
array of allocate memory for it.

Nov 7 '05 #2

P: n/a
batista wrote:
Hi,

I am getting the following exception

First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation

When i run the following code

1 int main(void)
2 {
3 char* str1 = "MA";
4 char s = str1[0];
5 str1[0] = 'r';
6 return 0;
7 }

i'm getting exception on line 5. Why?

Bye.


becouse "MA" is a string iteral, and as such cant be modified, even
though its possilble to get a non const pointer to it for some bizzare
reason

your code is more less equivalent of

((char*)"MA")[0] = 'r';

if you want to get a copy of "MA" what you want to do is
char str1[] = "MA";
now str1[0] is modifiable
Nov 7 '05 #3

P: n/a
batista wrote:
Hi,

I am getting the following exception

First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation

When i run the following code

1 int main(void)
2 {
3 char* str1 = "MA";
4 char s = str1[0];
5 str1[0] = 'r';
6 return 0;
7 }

i'm getting exception on line 5. Why?

Line 3 should read:

const char* str1 = "MA";

It is specifically allowed by the standard as your original because of
the volume of legacy code that would otherwise break. (IMHO a bad move
by the standards comittee.)

You can however rewrite line 3 like:

char str1[] = "MA";

.... now you're free to change the three characters that is str1.
Nov 7 '05 #4

P: n/a
In article <11*********************@g49g2000cwa.googlegroups. com>,
batista <sa*********@gmail.com> wrote:
Hi,

I am getting the following exception

First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation

When i run the following code

1 int main(void)
2 {
3 char* str1 = "MA";
4 char s = str1[0];
5 str1[0] = 'r';
6 return 0;
7 }

i'm getting exception on line 5. Why?


Check out http://www.comeaucomputing.com/techtalk/#stringliteral
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 8 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.