delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/08/29/08:00:11

From: Martin Ambuhl <mambuhl AT earthlink DOT net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
X-Accept-Language: en-us, en, de, fr, ru, el, zh
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: struct bug?
References: <bin4ql$sfh$1 AT newsreader1 DOT netway DOT at>
In-Reply-To: <bin4ql$sfh$1@newsreader1.netway.at>
Lines: 71
Message-ID: <z3H3b.368$Om1.335@newsread2.news.atl.earthlink.net>
Date: Fri, 29 Aug 2003 11:47:43 GMT
NNTP-Posting-Host: 66.90.172.27
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread2.news.atl.earthlink.net 1062157663 66.90.172.27 (Fri, 29 Aug 2003 07:47:43 EDT)
NNTP-Posting-Date: Fri, 29 Aug 2003 07:47:43 EDT
Organization: EarthLink Inc. -- http://www.EarthLink.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

News Reader wrote:

> /*
> The following short source does not compile,
> because Gcc obviously has problems with
> 
>     sizeof("structured type member")   .
> 
> Although there is plenty of workaraound (just use
> a variable instead of the type), I am still keen to
> understand why the compiler can handle the entire
> structure (e.g. 'abt' as stated below), but not a
> structure member (e.g. 'abt.a').
> 
> This looks like a bug to me, but I will gladly
> accept knowlegeable explanations of someone with
> more insight than myself.

It is not a bug in gcc, but in your understanding of the sizeof operator.
See below:
[// comments changed to newsgroup- and C89-friendly /* ... */]

 > <...>
 > typedef struct
 > {
 >     int a;
 >     int b;
 > } abt;
 >
 > <...>
 > abt ab;
 >
 > /*  Main() */
 >
 > int main()
 > {
 >     printf("\n\n");
 >
 >     printf("Sz(ab):    %ld\n", sizeof(ab)); /* These compile, no
 >                                                problem */
 >     printf("Sz(ab.a):  %ld\n", sizeof(ab.a));
 >     printf("Sz(abt):   %ld\n", sizeof(abt));    /* this still
 >                                                    compiles */
 >
 >     printf("Sz(abt.a): %ld\n", sizeof(abt.a));  /* >>> BUT THIS
 >                                                    ONE DOESN'T */

abt is a type.  sizeof is not defined for members of a type,
but is for members of an object.  Relevant to this is this from
6.5.3.4  of the draft version of the C99 standard:

     6.5.3.4  The sizeof operator

     Semantics

     [#2]  The  sizeof operator yields the size (in bytes) of its
     operand, which may be an  expression  or  the  parenthesized
     name of a type.  The size is determined from the type of the
     operand.  The result is an integer.   If  the  type  of  the
     operand  is  a  variable  length  array type, the operand is
     evaluated; otherwise, the operand is not evaluated  and  the
     result is an integer constant.

Note that (ab) and (ab.a) are expressions, (abt) is a parenthesized name of a
type, but (abt.a) is neither.



-- 
Martin Ambuhl

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019