Macro problem

This is a discussion on Macro problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a bit of trouble with a macro. Is it possible that having a macro that refers to ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Macro problem

    Hi, I'm having a bit of trouble with a macro. Is it possible that having a macro that refers to another macro which in turn referes to another macro and so on, too many times, can cause problems for the compiler? I'm cross compiling using AVR GCC, so I guess the compiler is gcc or some port of it. Anyway, here is the macros I got:

    Code:
    typedef  uint8_t  servo_time;
    #define  NUM_SERVO_TIME_BYTES  (sizeof servo_time)
    
    ...
    
    #define SERVO_DATA_MSG_LEN             (1 + NUM_SERVOS * NUM_SERVO_TIME_BYTES)
    
    ...
    
    #define MAX_MSG_LEN SERVO_DATA_MSG_LEN
    
    ...
    
    #define I2C_MAX_DATA_LEN MAX_MSG_LEN
    NUM_SERVOS is an enum. When the program gets down to these rows:

    Code:
        if (read_buf->data_len < I2C_MIN_DATA_LEN ||
            read_buf->data_len > I2C_MAX_DATA_LEN)
        {
    I get: "error: expected expression before 'servo_time'" at the second rows. However, when I replace the definitions with what they are defined as, i.e.:

    Code:
        if (read_buf->data_len < I2C_MIN_DATA_LEN ||
            read_buf->data_len > (1 + NUM_SERVOS * (sizeof servo_time)))
        {
    the problem ceases to exist. Anybody has any idea of why?
    Come on, you can do it! b( ~_')

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There should be a switch in your compiler (in gcc it's -e) that says "run only the preprocessor" -- you can then see what the compiler is actually seeing. (You do have to be careful since macro substitution only goes forward, but at first glance you appear to have that order correct.)

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Thumbs up

    I will have to check that up tomorrow when I'm back to school and the same compiler; could be fun to know why it does this.
    Come on, you can do it! b( ~_')

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >the problem ceases to exist
    I really have no idea why does it cease to exist. As to my knowledge, using sizeof with a typename always requires putting it in parentheses.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Yeah, GL.Sam is correct. Put parentheses around "servo_time" to fix the compile error. You are allowed to leave off the parentheses when sizeof takes an expression, but not when it takes the name of a type.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I did put parenthesis around the type, but it didn't matter. I still got the same error message. But now I'm not sure about whether I actually saved the file or not after I made the changes ... I'm used to programming in Visual Studio, which always saves all the files you have been working on before you use them in a compilation (AVR Studio does this to, but for some reason only for source files), or in Emacs which asks you for every file you have changed but not saved if you want to save it or not. Great, now I'm going to try that to tomorrow!
    Come on, you can do it! b( ~_')

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Thumbs up

    GL.Sam, you were right. When I put parenthesis around servo_time and just saved it worked. Thanks!
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in macro
    By Bargi in forum C Programming
    Replies: 17
    Last Post: 02-04-2009, 09:17 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread


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