News for hackers. Stuff that matters.
Re:A convenient untruth: Array notation in C is a lie by wbkang on Thursday December 22, 2016 @11:03AM (#13238136)
In the last example,

> char string[] = "Hello world";

I thought that gives out warning these days. Isn't the proper type of a string literal the following?

> const char string[] = "Hello world";

Therefore, you can't really modify individual characters there.

Re:A convenient untruth: Array notation in C is a lie by kbp on Thursday December 22, 2016 @11:12AM (#13238185)
You're confusing things a little bit. 'char s[] = "foo"' is equivalent to 'char s[] = { 'f', 'o', 'o', '\0' }' and gives you a normal mutable array (or you can make it const and then it's const; no surprises); it is okay to mutate that array. 'char * s = "foo"' gives you a pointer to a string literal that might be placed in read-only storage; in C++ it is a const char * (so modern compilers should complain about that initialisation), while in C it is a char * where it's UB if you modify it (compilers may warn about storing it in a non-const char *; many don't).
Re:A convenient untruth: Array notation in C is a lie by coldpie on Thursday December 22, 2016 @11:15AM (#13238204)
> Isn't the proper type of a string literal the following?

No, it's a const char pointer. Your declaration actually makes a copy of the "Hello world" string into a new char array, distinct from the string literal itself:

  [~]$ cat test.c
  #include <stdio.h>
  
  int main(int argc, char ** argv)
  {
      char a[] = "test2";
      printf("sizeof(a): %lu\n", sizeof(a));
      return 0;
  }
  [~]$ gcc -std=c99 -pedantic -Wall -o test test.c
  [~]$ ./test
  sizeof(a): 6
  [~]$
Re:A convenient untruth: Array notation in C is a lie by copascetic on Thursday December 22, 2016 @12:41PM (#13238870)
The type of a string literal is array of char, and like other arrays, they decay to pointers to the first element when used in expression context, with three exceptions: sizeof, taking the address with &, and when used as a string initializer. "test2" in your example is not a const char *, it's an initializer, since this is one of the exceptions to array-to-pointer decay.

http://c-faq.com/aryptr/aryptrequiv.html http://c-faq.com/decl/strlitinit.html

Re:A convenient untruth: Array notation in C is a lie by smitherfield on Thursday December 22, 2016 @03:38PM (#13240182)
C string literals are " char * ".

C++ string literals are " const char * " (one of the minor incompatibilities between the languages).

They ought to be "const char[]" but aren't. And, the behavior of "char[]" should be the same as "struct { char[] }", but it isn't.

Re:A convenient untruth: Array notation in C is a lie by gpderetta on Thursday December 22, 2016 @04:49PM (#13240677)
They actually are proper arrays in C++ [1], although, as usual, they decay to pointers faster than an unstable particle.

[1]: https://godbolt.org/g/ZWISCu

Re:A convenient untruth: Array notation in C is a lie by smitherfield on Friday December 23, 2016 @12:33AM (#13243075)
I stand corrected!