C
candide
Hi,
I try to find out what exactly means "an array and its address are the
same" or "have the same value".
Everybody seems to agree that they doesn't share the same type, of course.
For enlighting the discussion, here some more or less contradictory
quotes from clc and comp.std.c regarding this question :
-------------------------------- 8<
-------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
I try to find out what exactly means "an array and its address are the
same" or "have the same value".
Everybody seems to agree that they doesn't share the same type, of course.
For enlighting the discussion, here some more or less contradictory
quotes from clc and comp.std.c regarding this question :
-------------------------------- 8<
-------------------------------------------
> I'm not aware of any definition (or even description) of the C language
> that said that taking theaddressof anarraywas equivalent to taking
> theaddressof the first member of thearray. Certainly there were
> compilers that worked that way, but there were also many compilers that
> considered it an error and didn't allow it at all.
>
-------------------------------------------------------------------------------
------------------------------------------------------------------------------->> >(...) The address of an array IS the address of
> >it's first element.
> The standard doesn't say so explicitly although a conforming implementation
> doesn't really have much choice in the matter. (void *)&s and (void *)&s[0]
> pretty much have to generate thesameaddress or else standard library
> functions like fread() would have problems.
-------------------------------------------------------------------------------useful.> > OK, but what IS the address of an array? If we consider that in most
> > contexts an array name "a" is converted to &a[0], then the closest thing I
> > can imagine that would be anarray addressis the address of a cell that
> > holds &a[0]. Seems like that is just creating an unnecessary level of
> > indirection. I'd be interested in an example where this would be
>
> In the usual implementation the bit value of &a[0] and &a will be the
> same,
> but the type is different (ptr to int, ptr to array of int).
> If you add 1 to them you will get different answers.
> Don't be fooled by the fact that the above works. It just so happens that
> myarray and &myarray are both expressions that yield the same pointer. But the
> two pointers have a different type.
-------------------------------------------------------------------------------
> &array is a perfectly legitimate expression; it yields the address of
> the array, which is distict from the address of itsfirstelement.
> They have the "same value" in some sense, but they're of different
> types.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------> [...] The address of an array is the same as
> the address of its first element just as the address of a struct is the
> same as the address of its first member (the addresses are the same but
> the types are different)
--------------------------------- >8> Paul Seale wrote:
>
>
> What's theaddressof a struct? Theaddressof a struct is "the same
> as" theaddressof its first member, but they have different types.
> Likewise, theaddressof anarrayis "the same as" theaddressof its
> first element, but they have different types. (By "the same as", I
> mean that they compare equal if you convert them both to (void *)).