Search code examples
carrayspointersstring-literals

what is the technical difference between these declarations?


  char amessage[] = "now is the time"; /* an array */
  char *pmessage = "now is the time"; /* a pointer */

Solution

  • amessage is of type char[16]. It is an array. The elements of the array contain the sixteen characters from the string literal.

    pmessage is of type char*. It is a pointer. It points to a (non-modifiable) array containing the sixteen characters from the string literal. (You should avoid using a char* to point to a string literal; doing so is evil. You should always use a const char* wherever possible when referring to string literals.)