简体   繁体   中英

pointer to an array, difference in 2D array and 1d array

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM