What is the difference between char a[] = ?string?; and char *p = ?string?;?

前端 未结 8 1146
太阳男子
太阳男子 2020-11-22 07:43

As the heading says, What is the difference between

char a[] = ?string?; and 
char *p = ?string?;  

This question was asked to me in inter

8条回答
  •  深忆病人
    2020-11-22 07:57

    char *p = "string"; creates a pointer to read-only memory where string literal "string" is stored. Trying to modify string that p points to leads to undefined behaviour.

    char a[] = "string"; creates an array and initializes its content by using string literal "string".

提交回复
热议问题