Why does the sizeof
operator produces 12 bytes when it should only be 4? When I reference the variable array
, that is only referring to the memory address of the first index of the array. In fact, I printed the memory address of the first index &array[0]
and compared it to array
, they produced the same memory address result which confirms that they are both referring to the first index of the array, but 'array' produces 12 byte while array[0]
produces 4 byte.
int main() {
int array[] = {1,2,3};
int a = 1;
int b = sizeof(array); //this is referring to the first index of the array
int c = sizeof(array[0]); //this is also referring to the first index of the array
std::cout << b << std::endl;
std::cout << array << std::endl; //they have the same memory address
std::cout << &array[0] << std::endl; /* they have the same memory address, which confirms that array
and &array[0] is the same */
return 0;
}