1

I a 2D array say int a[2][3]

when we call the function say add(a);

we receive it using a pointer to an array void add(int(*p)[3])

BUT

In 1D array say int b[5]

we store address of array in a simple pointer to an integer int *p; p=b;

my question is that why don't we store b in a pointer to an array ex int(*p)[5]=b;

1 Answer 1

1

An array name when used as a value will decay into value equal to the pointer to its first element, with that type. This means for:

int a[2][3];

The name a will decay to &a[0], which has the type int (*)[3]. But, for:

int b[5];

The name b will decay to &b[0], which has the type int *.

However, &b is a pointer to b, which means it has the type int (*)[5]. It so happens that for an array type, its address will compare equal to the address of its first element. But, &b has a different type from &b[0].

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

Comments

Your Answer

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