sizeof(array)

David Gay gay%CLSEPF51.BITNET at CUNYVM.CUNY.EDU
Sat Apr 16 22:00:31 AEST 1988


In article <259 at sdrc.UUCP>, Larry Jones <scjones at sdrc.uucp> writes:
>In article <7684 at brl-smoke.ARPA>, gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
>> In article <8646 at eleazar.Dartmouth.EDU> major at eleazar.Dartmouth.EDU (Lou
> Major) writes:
>> >char foo[]="This is a test.";
>> >sizeof (foo) == sizeof (char *)
>>
>> [...]
>>
>> (I don't think the array name is turned into a pointer just because it's
>> surrounded by parentheses.)
>
>If it ain't, the compiler's broke!  The sizeof operator can be applied to a
>parenthesized type name or to an expression.  Since "foo" isn't a type name,
>the operand of sizeof is an expression.  When an array name appears in an
>expression and it's not the operand of & or sizeof (whose operand is the
>parenthesized express, remember), it's converted into a pointer to the first
>element.

But that isn't what it says; in K&R (1st edition), under the description of
sizeof, you see:

   " [...] When applied to an array, the result is the total number of bytes
    in the array."
    [Appendix A, 7.2, p188]
I would think that even with the array name surrounded by brackets,  sizeof
is still being applied to an array. Also, precisely this syntax is used when
sizeof is introduced (p126), the statement is

#define NKEYS (sizeof(keytab) / sizeof(struct key))

with

struct key { ... } keytab[] =
{
...
};

>
>----
>Larry Jones                         UUCP: uunet!sdrc!scjones
>SDRC                                MAIL: 2000 Eastman Dr., Milford, OH  45150
>                                    AT&T: (513) 576-2070
>"When all else fails, read the directions."
Precisely.
>

David Gay                                   GAY at CLSEPF51.bitnet

No one bears a responsability for my opinions.



More information about the Comp.lang.c mailing list