[We are given an array object named "p" -- which is not the best
name for an array, but never mind that

-- where p has type
(int [5][5]).]
>On Nov 20, 4:15 am, Chris Torek <nos...@torek.net> wrote:
>>... The "value" of p (p when converted from lvalue, or "object",
>to value) is a pointer value, pointing to the first element of the
>>array "p", i.e., the entire first row. ...
In article <713e5f01-4df9-4667-8e87->
somenath <> wrote:
>I am just getting confused here.
>If p points to all the 5 element of the first row why the sizeof(p) is
>100 ?
The sizeof operator suppresses object-to-value conversion.
In *most*, but definitely NOT all, contexts, an object of type
"array N of T" becomes a value of type "pointer to T", where N
is the size of the array and T is the type of the array elements.
In this case, N is 5, and T is (int [5]), or "array 5 of int".
The sizeof operator is one of those exceptions. The unary "&"
(address-of) operator is another. In both cases, applying these
operators to an array leaves the array as an array, and finds the
size of the array, or the address of the array, respectively.
The size of the array "p" is the size of 5 arrays of 5 arrays of
"int". If sizeof(int) == 4:
>Where sizeof(int) is 4 ?
then (sizeof p) is 100 (5 * 5 * 4).
>And sizeof(p+2) is equal to 4 ?
In this case, the operand of sizeof is p+2 -- not p -- so the
binary "+" operator gets the first crack at things. The binary
+ operator is one of the usual cases (remember, sizeof is one of
the UN-usual cases), so that we go from array object to pointer
value. Here we get the "value" of p, via The Rule about arrays
and pointers in C, so we have a pointer to the entire first row
of the array "p" (i.e., all of p[0]). The binary + operator moves
forward two rows, giving a pointer of type "int (*)[5]", pointing
to the third row of the array "p" (i.e., all of p[2]).
The sizeof operator thus has, as its operand, a value of type
"pointer to array 5 of int" (int (*)[5]); the size of such a pointer
is up to the compiler and/or machine, and in your case, is 4. (On
some machines this pointer has size 2 or 8; on a few, it even has
size 1. For instance, some DSP C compilers have 32-bit "C bytes".)
For additional illustration and/or confusion, try:
sizeof (&p)
sizeof (int (*)[5][5]) /* same as sizeof &p */
sizeof (int [5][5]) /* same as sizeof p */
sizeof (int (*)[5]) /* same as sizeof (p+0) or sizeof (p+i) */
sizeof (p[0])
sizeof (*p) /* same as sizeof p[0] */
sizeof (int [5]) /* same as sizeof p[0] */
The binding of the various operators is such that you can write
"sizeof &p" or "sizeof p[0]" without parentheses; however, note
that "sizeof (p + 0)" differs from "sizeof p + 0" because the
latter parses the same as "(sizeof p) + 0".
All of this makes more sense mentally, at least to me, if you
rewrite all your use of "p" (and this article) to use the name
"arr" instead.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.