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).
namedButtonStr[0]is of typeconst char*, so itssizeofis the size of pointer, not the array it points to.namedButtonStr, on the contrary, is an array, so itssizeofis the bytesize of the whole array, that is,3 * sizeof(<one item in the array>).Edit: By the way, this is a pretty standard idiom for determining array's size, you'll see it often.