Thread: Using & in #define

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    8

    Using & in #define

    I am trying to use some code from Ronald Mak's Writing Compilers & Interpreters:

    Code:
    #define enter_name_local_symtab(name, &symtab_display[level])  \
       idp = enter_symtab(name, &symtab_display[level])
    and this comes up with an error
    "&" cannot be used in a macro parameter list. I note that this is used quite a bit in the book (the book is quite old, so the C will be outdated).

    How do I get over this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You put the complexity in when you call the macro, not when you declare it.
    Code:
    #define enter_name_local_symtab(a, b)  \
       idp = enter_symtab(a,b)
    
    // then later, use the macro
    enter_name_local_symtab(name, &symtab_display[level])
    The macro parameters are just placeholders, to be replaced literally with the values used when you call the macro.
    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.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Have a look at: ftp://ftp.dante.de/tex-archive/support/ltx2x/l2xisymt.h where it has:

    Code:
    /**************************************************************************/
    /* enter_name_local_symtab() enter the given name into the local symtab   */
    /*                      and set pointer to the entry                      */
    #define enter_name_local_symtab(idp, name)                     \
      idp = enter_symtab(name, &symtab_display[level])
    /**************************************************************************/
    I don't particularly like that very much at all but it will at least work assuming there is a variable 'level' in scope (that's the stupid part I don't like)

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    Thank you Salem and Hodor. I'll look at your suggestions and see how they work before I progress further.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define
    By Johnathan1707 in forum C Programming
    Replies: 3
    Last Post: 08-02-2009, 09:10 AM
  2. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  3. #define
    By tikelele in forum C Programming
    Replies: 2
    Last Post: 12-01-2007, 11:43 PM
  4. using define
    By ygfperson in forum C Programming
    Replies: 1
    Last Post: 04-25-2002, 09:58 PM
  5. #define
    By face_master in forum C++ Programming
    Replies: 10
    Last Post: 09-07-2001, 02:53 PM

Tags for this Thread