Calculation of sizof through macro. Possible?

This is a discussion on Calculation of sizof through macro. Possible? within the C Programming forums, part of the General Programming Boards category; Hi, I need help on calculating sizeof through a macro. I have the folloing layout. In file "One.h" Code: typedef ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    Calculation of sizof through macro. Possible?

    Hi,

    I need help on calculating sizeof through a macro. I have the folloing layout.

    In file "One.h"
    Code:
    typedef uint16_t tTime;
    And in file "Two.h" I want to calculate as follwing:
    Code:
    #include "One.h"
    
    #define NODE_LENGTH 6
    #define TOT_SIZE NODE_LENGTH + sizeof(tTime)
    
    #if (TOT_SIZE > 100)
    #error "TOT_SIZE too big."
    #endif
    I get the following compiling error:
    Code:
    missing binary operator before '('
    How can I get pass this? I don't want to 'hard code' the TOT_SIZE.

    Thanks in advance,
    Indy

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Just remove the brackets around tTime.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    #if is evaluated by the pre-processor
    sizeof() is evaluated by the compiler.
    Ne'er the twain shall meet.

    You can do some funky things with templates in C++, but in C I think you're stuck with putting something like this in at the start of main.

    assert( !(TOT_SIZE > 100) );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    I guess I have to hard code it... can't use assert function... thanks though.

    Indy

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Rather than using #define, how about using a static const int? I've got a program somewhere that does something similar, let me have a look for it.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. L macro
    By George2 in forum C Programming
    Replies: 1
    Last Post: 08-20-2007, 09:24 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21