char s[] = "hello world";
char s[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};
Are these two lines of code equivalent in C++?
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.
No, the first one initialises the array by copying from a string literal which is stored in static memory. The second, which is initialised from an array initialiser list, probably doesn’t create anything in static memory, but generates instructions to move constant numbers (intrinsic to the instructions) into each position in the array.
Additionally, if two things are equal then applying the same transformation to them will also result in an equality. However, changing both to pointers instead of arrays (and adding const) will prevent the one with the initialiser list from compiling, so there they further differ.
Ignoring those differences, what you end up with in the array will be the same either way.