How it calculate size of array using pointer to array?

Viewed 92

I have this following code, which has pointer variable pointer to an 2D array.

int main()
{
    int (*ptr)[2][3];
    printf("%u\n", sizeof(ptr)); // ---> give size of pointer = 8
    printf("%u\n", sizeof(*ptr)); // ---> give size of array = 24, how it works adding * to ptr gives array size

    return 0;
}
3 Answers

You have to use the conversion specifier %zu with values returned by the operator sizeof that have the type size_t.

printf("%zu\n", sizeof(ptr)); 
       ^^^^^

If you have a pointer like

T *p;

where T is some type then the expression *p has the type T. So sizeof( *p ) is equivalent to sizeof( T ).

This declaration

int (*ptr)[2][3];

declares a pointer to the array type int[2][3]; You could rewrite the declaration with using a typedef the following way

typedef int T[2][3];
T *ptr;

So the expression sizeof( *ptr ) is equivalent to the expression sizeof( int[2][3] ).

sizeof(ptr) is giving the size of the poiter itself which is 8 in your system

sizeof(*ptr) is giving you the size of the object referenced by the pointer ptr. As ptr is a pointer to the 2D array of 6 integers (3x2) then the size of that array is 24 bytes assuming the sizeof(int) == 4

The expression ptr has type int (*)[2][3] (pointer to 2-element array of 3-element array of int) - it is a pointer type, so it's however big a pointer type is on your system (in this case, 8 bytes).

The expression *ptr has type int [2][3] - it's a 2x3 array of int, meaning it's big enough to store 6 int objects. An int is 4 bytes wide on your system, for a total size of 24 bytes.

If the operand of sizeof is an expression instead of a type name, it goes by the type of the expression. For example, the expression 1 has type int, the expression 2 has type int, and the expression 1 + 2 has type int, so

sizeof 1 == sizeof 2 == sizeof (1 + 2) == sizeof (int)

Remember that sizeof is an operator, not a function - you only need to use parentheses if the operand is a type name (like (int)) or if the operand is an expression containing operators of lower precedence. For example

sizeof 1 + 2

will be parsed as

(sizeof 1) + 2

because the arithmetic + operator has lower precedence than the unary sizeof operator, which is probably not what you want. However, expressions like

 sizeof *foo[N]

will be parsed as

 sizeof (*(foo[N]))

so parentheses aren't necessary in that case. Some people will advise you to always use parentheses regardless, which isn't bad advice; eventually you'll learn when they aren't necessary and can be comfortable leaving them out.

Related