C++ simple sizeof difference between char array and char pointer
问题 char * test = "test"; cout << sizeof(test); char test2[] = "test"; cout << sizeof(test2); Running this on visual studio 2010, why is the output 45 ? Shouldn't test be a string literal and the sizeof a string literal be the number of character elements in the string literal including the terminating null character? 回答1: test is a pointer to a string literal, not a string literal (a char[] ): the sizeof(char*) is 4 , relating to test the sizeof(char[5]) is 5 , relating to test2[] hence 45 is