In my C program book, I meet the question about (*p)[num2]
there is a 2 dimension array named a[num1][num2] and a (*p)[num2]
next,in the statement
for(p=&a[0];p<&a[num1];p++)
(*p)[i]=0;
that statement will make the 2 dimension array's i column assign 0
but i don't understand why (*p)[num2] point to a's row,I think create *p and p=&a[num1] can do that,too.
and,why p++ point to next row but not the next value behind a[num][i]
why?,i have understand the difference between *p[3] and (*p)[3]
I think *p could point to an array(first address) why used (*p)[num] to point array.
Let's start from providing a useful quote from the C standard relative to arrays and implicit conversions (6.3.2.1 Lvalues, arrays, and function designators)"
So if for example you have an array like that
then used in expressions with rare exceptions mentioned in the quote it is implicitly converted to a pointer to its initial element.
Elements of the two-dimensional array
aare in turn arrays of the typeint[num2]. A pointer to the element type of the array has typeint ( * )[num2]. You may declare a corresponding pointer the following waythat is the same as
Now the pointer
ppoints to the first "row" of the two-dimensional arraya. After incresing it using the prefix increment operator++por postfix increment operatorp++it will point to the next (second) element (row) of the typeint[num2]of the arraya. Dereferencing the pointer like*pyou will get the one-dimensional array (row) of the typeint[num2]pointed to by the pointer. And to apply to the resulted expression the subscript operator like( *p )[i]you will get thei-thelement of the current row of the arraya.On the other hand, if you have
then the expression
p + num1will point to the memory after the last element (one-dimensional array of the typeint[num2]) of the arraya. The expressionp + num1is equivalent to the expressiona + num1(due to the implicit conversion of the arrayato a pointer to its initial element) that in turn is equivalent to the expression&a[num1]. According to the last expression there is written in the C Standard (6.5.3.2 Address and indirection operators)So as it follows from the quote the expression
&a[num1]is equivalent to the expressiona + num1.Thus this for loop
may be rewritten like
As for the subscript operator then (the C Standard, 6.5.2.1 Array subscripting):
Thus this expression statement in the body of the for loop
may be also rewritten like
or like