This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: sizeof() function parameter array: known problem?
- From: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- To: Etienne Lorrain <etienne_lorrain at yahoo dot fr>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 1 Jul 2005 10:17:04 +0100
- Subject: Re: sizeof() function parameter array: known problem?
- References: <20050701084519.9199.qmail@web26907.mail.ukl.yahoo.com>
On Fri, Jul 01, 2005 at 10:45:19AM +0200, Etienne Lorrain wrote:
> The result of this funtion is 1, is there a C lawyer around?
The parameter is treated as unsigned* since an array is converted to
a pointer when passed through a function.
C99 says in 6.7.5.3:
[#7] A declaration of a parameter as ``array of type'' shall
be adjusted to ``qualified pointer to type'', where the type
qualifiers (if any) are those specified within the [ and ]
of the array type derivation.
> $ cat tmp.c
> unsigned fct (unsigned array[10])
These prototypes are all equivalent, and any sized array (or just a
plain pointer) can be passed to them:
unsigned fct (unsigned array[10]);
unsigned fct (unsigned array[]);
unsigned fct (unsigned* array);
> {
> return sizeof(array) / sizeof(array[0]);
> }
Therefore what you're testing is sizeof(unsigned*)/sizeof(unsigned)
which is 1 on x86 and most other 32-bit targets (but 2 on e.g. x86_64)
Hope that helps,
jon