Why sizeof(str) = 24? char *str[] = {"a", "ab", "cd"};

Ask a question+

Thought it would be 3, the size of the array, but the answer is 24, size of 3 pointers.

add comment

1 Answer


The sizeof operator on an array gives the size of the whole consecutive array, which is the number of elements times the size of each element.

So for your case 24 = 3 * 8.

The size of the pointer on your system is 8 (64-bit).
Try to print it

    char *p = "Something";
    std::cout << sizeof(p) << std::endl; // 8

Here is the ‘classical’ way in C of figuring out the number of elements

int size = sizeof(arr)/sizeof(arr[0]);
Tigran 1.2K

Got it, thanks~

[email protected]
add comment

Your Answer