c - why does derefrencing a pointer to an array of integers(in 2d array) return(or decay to) pointer to first element? -
i have read many posts of pointers , 2d array relation, cant seem understand concept. lets there 2d array int a[3][2] , array int b[2].
a returning pointer array of integers of size 3. of type int (*)[2].
as understanding of concept goes derefrencing it(*a) give me array , this decays pointer pointing first element of 1d array , of type (int*) . bold part doubt.
why , how decay happen array itself(which complete 1d array a[0]) decays first element of 1d array?
(cant seem why , how part) , cant seem find on other links also.
as a[0], *a,&a[0][0] represent same pointer. here a[0] first 1d array. true b(declared above) , a[0] represent same thing (both being 1d array , decay pointer first element in array of type (int*)?
why , how decay happen array decays first element of 1d array?
c11: 6.3.2.1 p(3):
except when operand of
sizeofoperator,_alignofoperator 1,the unary&operator, or string literal used initialize array, expression has type ‘‘array of type’’ converted expression type ‘‘pointer type’’ points initial element of array object , not lvalue.
in simple; array names can converted pointer first element.
since a array name, decays pointer first element 1d array. type int(*)[2]. since a (after decay) pointer array of 2 int, *a array of 2 ints. since that's array type, i.e *a array name first 1d array, decays pointer first element (a[0][0]) of array object. so, type int *.
is true
b(declared above) ,a[0]represent same thing (both being 1d array , decay pointer first element in array of type (int*)?
yes b , a[0] both of type int * (after decay).
1. read commat keith thompson. read answer states that: a draft of c11 standard says there's exception arrays, namely when array operand of new _alignof operator. error in draft, corrected in final published c11 standard; _alignof can applied parenthesized type name, not expression.
Comments
Post a Comment