From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14045 invoked by alias); 1 Jul 2005 09:17:12 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 13986 invoked by uid 22791); 1 Jul 2005 09:17:06 -0000 Received: from serenity.mcc.ac.uk (HELO serenity.mcc.ac.uk) (130.88.200.93) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Fri, 01 Jul 2005 09:17:06 +0000 Received: from compsoc.umu.man.ac.uk ([130.88.22.5] helo=mrtall.compsoc.man.ac.uk) by serenity.mcc.ac.uk with esmtp (Exim 4.43 (FreeBSD)) id 1DoHdw-000Hoz-6U; Fri, 01 Jul 2005 10:17:04 +0100 Received: from cow by mrtall.compsoc.man.ac.uk with local (Exim 3.36 #1) id 1DoHdw-000ItQ-00; Fri, 01 Jul 2005 10:17:04 +0100 Date: Fri, 01 Jul 2005 09:17:00 -0000 From: Jonathan Wakely To: Etienne Lorrain Cc: gcc@gcc.gnu.org Subject: Re: sizeof() function parameter array: known problem? Message-ID: <20050701091703.GA71453@compsoc.man.ac.uk> References: <20050701084519.9199.qmail@web26907.mail.ukl.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050701084519.9199.qmail@web26907.mail.ukl.yahoo.com> User-Agent: Mutt/1.4.2.1i X-UoM: Scanned by the University Mail System. See http://www.mcc.ac.uk/cos/email/scanning for details. X-SW-Source: 2005-07/txt/msg00027.txt.bz2 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