I am confused about the sizeof string array in C++, I have the following string array:
static const char* namedButtonStr[] = {
"GLUT_LEFT_BUTTON",
"GLUT_MIDDLE_BUTTON",
"GLUT_RIGHT_BUTTON"
};
And to get the size of this array, the following code is used:
int size = int(sizeof(namedButtonStr)/sizeof(namedButtonStr[0]));
Where sizeof(namedButtonStr) is 12, sizeof(namedButtonStr[0]) is 4, and the size of the array is 12/4 = 3.
My question is, why sizeof(namedButtonStr) is 12 and sizeof(namedButtonStr[0]) is 4? My understanding is sizeof(namedButtonStr) is 3 and sizeof(namedButtonStr[0]) is 17 ("GLUT_LEFT_BUTTON" has 17 characters).
std::vector<std::string> namedButtons = {"GLUT_LEFT_BUTTON", "GLUT_MIDDLE_BUTTON", "GLUT_RIGHT_BUTTON"};. In this case, the number of strings is justnamedButtons.size().namedButtonsis an array of pointers, not an array of strings. Each element points to the first character of a string.OpenGL, this code came from my teacher, there must be some reason he created the string array like that, can you explain it?