2

I searched many similar questions, but none of them actually answer my question. I know the difference between char* and char[]. char* is a pointer, which can point to a string, and can be reassigned to another string, like this:

char* p;
p = "hello";
p = "world";

but the string p points to cannot be modified. char[] is an array, the element of the string in this array can be modified, like this:

char[] arr = "hello";
arr[0] = 'w'; // arr = "wello";

my question is that why arr cannnot be reassigned? like this:

arr = "world";

what is the relation between the name arr and the actual char[]? Based on my understanding, arr points to the starting position of char[], which means it is similar to a pointer, like p in the first example, but it also seems that arr itself does not have a memory address, unlike p. So, basically, I am quite confused about why arr cannot be assigned like a pointer but works like a pointer. I am looking forward to very detailed explanantions, thanks for help.

zangsy
  • 35
  • 5
  • 2
    https://stackoverflow.com/a/8940392/918959 has the answer. It is legacy. C was supposed to replace the language called B and many of its features are so today because they aided in porting B programs to C. Some things like the precedence of [`&` and `|` do make zero sense this day as they are](https://stackoverflow.com/questions/4685072/operator-precedence-bitwise-lower-than#comment66723659_4685140), but they cannot be fixed *now*, because they could break so many existing programs. – Antti Haapala -- Слава Україні Nov 20 '19 at 16:40

0 Answers0