Thread: what is wrong with the macro definition....

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    what is wrong with the macro definition....

    Hi,

    I have following:

    Code:
    #define JPDM_NAME_SIZE 16
    
    #define JPDM_DECLARE_BUFFER_DESCRIPTION(name, ptr, size) \
        {                                           \
            JPDM_RECOVERY_STATE_NONE,               \
            name,                                   \
            (ptr),                                  \
            (size),                                 \
            NULL,                                   \
            0                                       \
        }
    
    typedef enum
    {
        JPDM_RECOVERY_STATE_NONE,
        JPDM_RECOVERY_STATE_NEW,
        JPDM_RECOVERY_STATE_RECOVERED,
        JPDM_RECOVERY_STATE_SAVED
    } teRecoveryState;
    
    
    //Following statement gives error
    JPDM_DECLARE_BUFFER_DESCRIPTION("Anoop",&sSwitch,sizeof(tsSwitch));
    Following macro definition gives me error:

    error: expected ';' before '}' token

    What is wrong in following statement?


    Thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The backslash doesn't precede but follows the opening brace, as in
    Code:
    #define JPDM_DECLARE_BUFFER_DESCRIPTION(name, ptr, size) { \
        JPDM_RECOVERY_STATE_NONE,   \
        name,                       \
        (ptr),                      \
        (size),                     \
        NULL,                       \
        0;                          \
    }
    [Edit] And a semi-colon at the end as all statements are separated by commas.
    Last edited by itCbitC; 04-27-2010 at 08:48 AM. Reason: added semi-colon (in red)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    The backslash doesn't precede but follows the opening brace, as in
    That should not be a problem though, since the line with the opening brace has its own backslash.

    sanddune008, I suggest that you post the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    That should not be a problem though, since the line with the opening brace has its own backslash.
    Yep! good catch, and for some reason I totally missed that.
    It's the comma operator that needs a semi-colon terminator.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Don't know but could the problem be that you simply need to put the macro into context? eg:
    Code:
    #include <stdio.h>
    
    #define JPDM_NAME_SIZE 16
    
    #define JPDM_DECLARE_BUFFER_DESCRIPTION(name, ptr, size)\
           { \
             JPDM_RECOVERY_STATE_NONE, \
             name,                     \
             (ptr),                    \
             (size),                   \
             NULL,                     \
             0                         \
           }
    
    typedef enum
    {
        JPDM_RECOVERY_STATE_NONE,
        JPDM_RECOVERY_STATE_NEW,
        JPDM_RECOVERY_STATE_RECOVERED,
        JPDM_RECOVERY_STATE_SAVED
    } teRecoveryState;
    
    struct BuffS { int state1;     /* #### just guessing at this struct name and members... */
                   char * name;
                   void * p1;
                   size_t sz;
                   void*  p2;
                   int state2;
                  };
    
    int main(void)
    {
      int  sSwitch = 5;   /* ####  guessing at these vars too */
      char tsSwitch[] = "And the last to go will get to watch the others go before her... hahahaha";
    
      /* Following statement gives error  #### see how it now is a struct initializtion?  */
      struct BuffS bf1 = JPDM_DECLARE_BUFFER_DESCRIPTION("Anoop", &sSwitch, sizeof(tsSwitch));
    
      printf("bf1.state1=%d  name=%s  p1=%p  sz=%u  state2=%d  &sSwitch= %p \n",
              bf1.state1, bf1.name, bf1.p1, bf1.sz, bf1.state2, (void*)&sSwitch );
    
      return 0;
    }
    /*
    ~>  gcc -Wall -W -pedantic 100427_macro-00.c -o 100427_macro-00
    100427_macro-00.c: In function ‘main’:
    100427_macro-00.c:37: warning: initializer element is not computable at load time     #### refers to:  &sSwitch 
    100427_macro-00.c:37: warning: unused variable ‘bf1’
    ~>  
        #### no segfault at run
    ~>  ./100427_macro-00
    bf1.state1=0  name=Anoop  p1=0xbfeea0f4  sz=74  state2=0  &sSwitch= 0xbfeea0f4
    ~>  
        #### a look at the expanded macro:
    ~>  cc -E 100427_macro-00.c -o 100427_macro-00
    ~>  vi 100427_macro-00
    ...
    int main(void)
    {
      ...
      struct BuffS bf1 = { JPDM_RECOVERY_STATE_NONE, "Anoop", (&sSwitch), (sizeof(tsSwitch)), ((void *)0), 0 };
    
      return 0;
    }
    */
    I guess it works? Good exercise anyhow.
    Last edited by HowardL; 04-27-2010 at 11:11 AM.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Code:
    Following in header file
    
    #define JPDM_DECLARE_BUFFER_DESCRIPTION(name, ptr, size) \
        {                                           \
            JPDM_RECOVERY_STATE_NONE,               \
            name,                                   \
            (ptr),                                  \
            (size),                                 \
            NULL,                                   \
            0                                       \
        }
    
    
    typedef enum
    {
        JPDM_RECOVERY_STATE_NONE,
        JPDM_RECOVERY_STATE_NEW,
        JPDM_RECOVERY_STATE_RECOVERED,
        JPDM_RECOVERY_STATE_SAVED
    } teRecoveryState;
    
    typedef struct sJPDM_BufferDescription
    {
        teRecoveryState eState;
    
        char acName[JPDM_NAME_SIZE];
        void  *pvBuffer;
        uint32 u32BufferSize;
    
        struct sJPDM_BufferDescription *psNext;
        uint16 u16OffsetInFlash;
    
    } tsJPDM_BufferDescription;
    
    
    Following in source file
    
    typedef struct
    {
        teBindType eBound;
        uint64 sDestAddr;
        uint64 sParentAddr;
        bool_t bAppTimerStarted;
    }
    tsSwitch;
    
    
    //Application
    tsJPDM_BufferDescription sDescription;
    
    PUBLIC void vCbConfigureNetwork(void)
    {
    	sDescription = JPDM_DECLARE_BUFFER_DESCRIPTION("Anoop",&sSwitch,50);
    
    	// Set PAN_ID and other network stuff or defaults will be used
           Channel = 0x11;
    
    }

    error:

    /cygdrive/c/js/Application/LightSwitch/LightSwitch/Source/LightSwitch.c: In function 'vCbConfigureNetwork':
    /cygdrive/c/js/Application/LightSwitch/LightSwitch/Source/LightSwitch.c:148: error: expected expression before '{' token

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can only make full on assignments at the time of declaration. You can't just do:
    Code:
    struct foo bar;
    
    bar = { a, b, c, d }; <-- you can't do that
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks quzah......
    Thanks all for your time....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wad wrong with this code?
    By tantan23434 in forum C Programming
    Replies: 3
    Last Post: 10-06-2009, 11:34 PM
  2. What am I doing wrong ?
    By scottjge in forum C Programming
    Replies: 6
    Last Post: 10-01-2009, 11:55 AM
  3. Multiple Definition Problem
    By EASports in forum Linux Programming
    Replies: 2
    Last Post: 07-14-2009, 11:42 AM
  4. what's wrong with this map definition?
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 06:17 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM