I know this is a very basic question. I am confused as to why and how are the following different.
char str[] = "Test";
char *str = "Test";
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Is an array of
chars, initialized with the contents from “Test”, whileis a pointer to the literal (const) string “Test”.
The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of
"Test", while the pointer simply refers to the contents of the string (which in this case is immutable).