From: Jonathan Wakely <cow@compsoc.man.ac.uk>
To: Etienne Lorrain <etienne_lorrain@yahoo.fr>
Cc: gcc@gcc.gnu.org
Subject: Re: sizeof() function parameter array: known problem?
Date: Fri, 01 Jul 2005 09:17:00 -0000 [thread overview]
Message-ID: <20050701091703.GA71453@compsoc.man.ac.uk> (raw)
In-Reply-To: <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
prev parent reply other threads:[~2005-07-01 9:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-01 8:45 Etienne Lorrain
2005-07-01 9:17 ` Andrew Haley
2005-07-01 9:17 ` Jonathan Wakely [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050701091703.GA71453@compsoc.man.ac.uk \
--to=cow@compsoc.man.ac.uk \
--cc=etienne_lorrain@yahoo.fr \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).