0
char i[6] = "Hello"; //EXAMPLE 1
const char *p =   "Hello"; //EXAMPLE 2 
char q[6] = i; //EXAMPLE 3 
char s[6] = (*(&i)); //EXAMPLE 4

"Hello" is a string literal, which has a type of const char[6] which is converted to const char* ( like in EXAMPLE 2 )

1 - Why does not this conversion happen in EXAMPLE 1 ?

2 - How can a value of type const char[6] be used to initialize an entity of type char[6] ( in EXAMPLE 1 ) ?

3 - EXAMPLE 3 will cause an error because of Array-to-pointer decay , right ?

4 - EXAMPLE 4 will cause an error because of Array-to-pointer decay , right ?

f877576
  • 447
  • 2
  • 7
  • In example 1, elements of the string literal are used to initialize elements of the array. It is equivalent to writing `char i[6] = {'H', 'e', 'l', 'l', 'o', '\0'};`. That is, in example 1 there is no conversion. This answers you 1st and 2nd question. Moreover, in example 3 `i` decays to `char*` which cannot be used as initializer to initialize a `char [6]` array. – Jason Jul 02 '22 at 05:14
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/246132/discussion-on-question-by-f877576-string-literal-and-char-array). – Ryan M Jul 03 '22 at 09:39

0 Answers0