0

I was under the impression that

    char word[5] = "hello";

is the same as

    char *word = "hello"; 

because arrays always decay into a pointer?

Jules
  • 423
  • 3
  • 11
  • 3
    `char word[5] = "hello";` can't be treated as a string, it's a normal character array. `word` needs to have space for 6 characters to make it a proper C string (The trailing 0 is critical). – Shawn Dec 07 '19 at 00:13
  • 2
    `char* word = "hello"` gets compiled into a different read-only section of your program - here's an answer that goes into more detail: https://stackoverflow.com/a/2589963/1158478 – ArtHare Dec 07 '19 at 00:15

1 Answers1

0

char *word = "hello"; Takes the address of a string literal. These are often held in read-only memory.

foreverska
  • 585
  • 3
  • 20