0

Possible Duplicates:
Is it possible to modify a string of char in C?
What is the difference between char s[] and char *s in C?

There is something I don't understand about strings and pointers in C.

Suppose I have this declaration:

char str[] = "abc";

Then, if I attempt to modify it this way:

str[0] = 'b';

It will work.

But if I declare the string as a pointer to a char

char* str = "abc"

The attempt above will cause an access violation.

What I'm trying to understand is what exactly is the difference.

Thanks in advance

Community
  • 1
  • 1
Ben
  • 43
  • 3
  • 5
    Please search before asking - this has to have been answered 50K times here (plus 20M more on the interwebs) – KevinDTimm Feb 01 '11 at 15:16

1 Answers1

0

In The later example the compiler puts the string in the read only data section, so u can't modify it. But in earlier example you are declaring array of character which resides in stack hence you can modify content of this array.

crypted
  • 10,118
  • 3
  • 39
  • 52