Re: [C] Array bounds in function prototypes

From: Jason Spashett (not-for-mail_at_b.com)
Date: 03/10/04


Date: Wed, 10 Mar 2004 20:11:47 -0000


"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:4kK3c.13371$%06.1728@newsread2.news.pas.earthlink.net...
...
> Well, just like it says, an array argument 'decays' into a pointer
...

Thanks for the detailed information. However, exactly why does it decay to a
pointer in a function prototype? I can't see why the array typing
information could not be preserved, is it for backwards compatibility? [ And
as an aside: is this the same in C++, I would presume not ]

I realised that I could pass a pointer to an array of n elements to do the
job, I've never actually passed fixed sized array before. I normally just
say char *map instead.

In the case of printf("%u", sizeof x); won't x get promoted to unsigned int
anyway, even if one does cast a size_t type to some unsigned type it may not
work anyway (in theory) ?



Relevant Pages

  • Re: Out-of-bounds nonsense
    ... a, i.e. it is a pointer to an array of two int, and it has type int. ... It decays as you specified, but you are allowed to use it to access all of the memory allocated by the 2D array declaration. ... the pointer that foo decays to is guaranteed to be correctly alligend ...
    (comp.lang.c)
  • Re: Out-of-bounds nonsense
    ... decays to a pointer to the start of entire array of arrays ... a, i.e. it is a pointer to an array of two int, and it has type int. ...
    (comp.lang.c)
  • Re: [Lit.] Buffer overruns
    ... which may have a different size or representation. ... The other expressions are pointer values, ... Hence, "x" decays automatically ... yields the address of its operand, whether it is an array or not. ...
    (sci.crypt)
  • Re: Is the name of a 2-dimensional array equal to address of [0][0] element?
    ... >pointer type. ... an array's name refers to the array. ... array expression "decays" to a pointer to its first element. ... However, just as an array of N int decays to a pointer to int, an ...
    (comp.lang.c)
  • Re: [C] Array bounds in function prototypes
    ... >> Well, just like it says, an array argument 'decays' into a pointer ... > Thanks for the detailed information. ... I can't see why the array typing ...
    (alt.comp.lang.learn.c-cpp)