-1

As far as I know the below code should not work. Yet, somehow this is OK on my compiler. Please could someone explain.

int main()
{
    char *string;
    string = "Goo";
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

3

As far as I know the below code should not work

I'm afraid, your information is wrong.

char *string;
string = "Goo";

is perfectly valid. This is basically,

  1. Define a char pointer string.
  2. Put the base address of the string literal "Goo" into string.

However, instead of being a char pointer, if string would have been an array, then this would not have been possible as array's cannot be assigned (except definition time though brace enclosed list).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Plus for legacy reasons, "Goo" is not `const` unless your compiler is gcc and you explicitly tell it to make them const. – Medinoc Jun 22 '15 at 19:12
  • 1
    @Medinoc ....(just for completeness), but, (attempt to) modifying them is anyway [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). – Sourav Ghosh Jun 22 '15 at 19:20
  • 1
    `-Wwrite-strings` is what changes string literals to have `const char *` – o11c Jun 22 '15 at 19:28
  • 1
    @o11c Nice one, but that's maybe again `gcc` specific, IMHO, not standard `C`. Hope I'm not misunderstood. :-) – Sourav Ghosh Jun 22 '15 at 19:30
  • Mostly I was extending @Medinoc 's comment. But IMHO since the only standard-compliant compilers implement a significant subset of GCC extensions, GCC-specific extensions are reasonably allowed. – o11c Jun 22 '15 at 19:33
  • 3
    @o11c: It changes them to `const char[N]`, not to `const char *`. However, this is a violation of C language spec. In C string literals are not const-qualified. – AnT stands with Russia Jun 22 '15 at 19:34
  • "... if string would have been an array, then this would not ...". How about a _compound literal_? – too honest for this site Jun 22 '15 at 21:17
  • @Olaf Good Morning Sir !! Yeah, you're right, but by saying "this" , I only meant this particular assignment, in current form. :-) – Sourav Ghosh Jun 23 '15 at 06:24
  • Thanks for the input Sourav + others. I realise that I was confusing char* with char x[]! – user3902889 Jun 23 '15 at 08:38
  • @user3902889 You're welcome . :-) BTW, you can consider [accepting](http://meta.stackexchange.com/q/5234) an answer that helped you. – Sourav Ghosh Jun 23 '15 at 09:03
  • (Good day to you, too Sir) Ok, I think I got your point. Yes a simple assignment is not possible, you would need `memcpy()` then. – too honest for this site Jun 23 '15 at 10:20
  • @AnT: You are right, this is non-standard. That is a gcc-specific warning/modifier (it should not start with `-W` actually. It is not included in `-Wall` for that reason. However, as for embedded systems one very likely wants to pack string literals into Flash, this is a very welcome feature. It also warns if breaking const-correctness by assigning such a literal to a non-`const char *. – too honest for this site Jun 23 '15 at 10:27
  • @SouravGhosh, done! I'm still getting used to this whole stackexchange thing. Thanks for your help! – user3902889 Jun 24 '15 at 04:48