2

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;
}
2
  • 4
    sizeof(*ptr) gives you the size of what ptr points to, which is an array of 2×3 ints. So it should be the same as int a[2][3]; sizeof(a), as it looks like it is. Commented Sep 14, 2021 at 19:17
  • do you mean size (24 bytes) or rather shape (2-by-3) ? Commented Sep 14, 2021 at 20:11

3 Answers 3

3

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] ).

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.