• AVR Freaks

Hot!sizeof(struct_name.field) vs. sizeof(struct_instance.field)

Author
sjb741
Super Member
  • Total Posts : 834
  • Reward points : 0
  • Joined: 2010/01/25 08:45:39
  • Location: 0
  • Status: offline
2020/06/20 09:08:19 (permalink)
0

sizeof(struct_name.field) vs. sizeof(struct_instance.field)

XC8 v2.20
typedef struct
{
   uint8_t blinkToggles; //Changes state of an LED
   uint8_t picClockIdx; //Relates to IRCF, backround timer pre & post-scaler
}ASTRUCT;

ASTRUCT aStruct;//Struct instance

volatile int i;
i = (sizeof(ASTRUCT)); //OK
i = (sizeof(aStruct.blinkToggles)); //OK
i = (sizeof(ASTRUCT.blinkToggles)); //error: expected ')'

According to this thread on Stack Overflow (Difference between sizeof(struct structname) and sizeof(object) in C), there is no difference for the structure as a whole. How about fields inside the structure? I get what seems to be a misleading error message when I try this with the structure type.
 
PS I cannot add the link as I get Access Denied errors
post edited by sjb741 - 2020/06/20 09:16:51
#1

6 Replies Related Threads

    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11891
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: sizeof(struct_name.field) vs. sizeof(struct_instance.field) 2020/06/20 10:20:58 (permalink)
    +2 (2)
    The operand of sizeof is a type or an expression.  ASTRUCT.blinkToggles is neither of those.  Use  sizeof aStruct.blinkToggles.
    #2
    1and0
    Access is Denied
    • Total Posts : 10906
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: sizeof(struct_name.field) vs. sizeof(struct_instance.field) 2020/06/20 10:45:50 (permalink)
    +2 (2)
    In other words, blinkToggles is a member of the struct aStruct, not of the type ASTRUCT; i.e. ASTRUCT.blinkToggles is not valid.
     
    #3
    andersm
    Super Member
    • Total Posts : 2822
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: sizeof(struct_name.field) vs. sizeof(struct_instance.field) 2020/06/20 11:43:09 (permalink)
    0
    A quick sniff test is to test your code with another compiler. Eg. Compiler Explorer lets you compile snippets using a lot of different compilers. If both GCC and MSVC reject your code, you can safely assume it really is invalid.
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11891
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: sizeof(struct_name.field) vs. sizeof(struct_instance.field) 2020/06/20 12:59:39 (permalink)
    +2 (2)
    If both GCC and MSVC reject your code, you can safely assume it really is invalid.

     
    Why would you think the compiler was generating an incorrect error message?  And trying another compiler wouldn't necessarily prove anything, since other compilers have different extensions that allow something not supported by XC8.
    #5
    andersm
    Super Member
    • Total Posts : 2822
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: sizeof(struct_name.field) vs. sizeof(struct_instance.field) 2020/06/20 13:35:25 (permalink)
    +2 (2)
    jtemplesWhy would you think the compiler was generating an incorrect error message?

    I don't, but many people are convinced they do. However, there's plenty of situations where it is useful to compare the behaviour of different compilers.
    #6
    JPortici
    Super Member
    • Total Posts : 1091
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: sizeof(struct_name.field) vs. sizeof(struct_instance.field) 2020/06/20 14:28:43 (permalink)
    +2 (2)
    Too many people think the compiler is bugged and / or producing incorrect code. The moment they realize their code is incorrect is the moment they have a chance to grow up (professionally speaking) and start writing good code :)
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5