Re: Order of evaluation.

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 02/11/04


Date: Wed, 11 Feb 2004 19:21:46 +0100

Jason wrote:

> "Richard Heathfield" <invalid@address.co.uk.invalid> wrote in message
> news:402a4aec@news2.power.net.uk...
>> Jason wrote:
>> > Good job I didn't write this then, which I believe is also valid, but
>> > unnecessary ( sizeof introduces the tag name a_struct in this context
>> > ):
>> >
>> > {
>> > size_t size = sizeof ( struct a_struct { int a; } );
>> >
>> > s = malloc( sizeof (a_struct) );
>> > }
>>
>> I think you might want to run this one by your compiler before you get
>> too fond of it. :-)
>
> Ah - it doesn't work then? I am not fond of it (never tried it). I read
> that in 1994, Samuel P. Harbison, et al., "C A Reference Manual", ISBN
> 0-13-326224-3, page 196
>
> "The effect of sizeof(struct S {int a,b;} ) is to create a new type [S] in
> ISO C, [cut], the type can be referenced later in the source file."
>
> Took me ages to find that reference.

It does not work, because in C a struct definition does not create a new
typename. Any struct-types you have defined must be referenced with the
keyword struct in place.
This code does work (at most it gives warnings about unused variables):

#include <stdlib.h>
int main()
{
  size_t size = sizeof ( struct a_struct { int a; } );
  void* s = malloc( sizeof (struct a_struct) );
}

Bart v Ingen Schenau

-- 
a.c.l.l.c-c++ FAQ: http://www.snurse-l.org/acllc-c++/faq.html (currently
unavailable)
a.c.l.l.c-c++ FAQ mirror: http://www.inglorion.com/acllcc++.html
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/


Relevant Pages

  • Re: List<> of struct with property. Cannot change value of property. why?
    ... method changes the struct that owns it. ... how would the compiler know the method changes the struct? ... Should the linker be required to carry this flag around too, so that when you import a reference to a struct type not compiled with the current project, you still have that information? ... There's a time and place for a value type, and in fact they even have their place in lists. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: List<> of struct with property. Cannot change value of property. why?
    ... method changes the struct that owns it. ... so that when you import a reference to a struct type not ... tell it specifically that it doesn't change it, then the compiler ... have their place in lists. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About reference
    ... > I got more confused regarding the size of a reference. ... > char c; ... In struct A there is no reference involved :-) ... It means there are situations where the compiler can do the substitution ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [PATCH] Use const* parameters in mm.h
    ... this gives the compiler wide space for optmizations. ... +static inline unsigned long page_zonenum(const struct page *page) ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • proxy_pda was Re: What was in the x86 merge for .20
    ... On Friday 08 December 2006 21:35, Jeremy Fitzhardinge wrote: ... in theory nothing should ever generate a reference to _proxy_pda. ... What compiler are you using? ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)