macros question #2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Arro

    #1

    macros question #2

    first of all i would like to thank Alexander Bartolich and Richard
    Heathfield for answering my
    "macros question" #1

    another macros question now:

    how could i define a macros with variable number of parameters? i want
    to create



    void printfatal(char *file, int line, char *format, ...);


    so i wanna define a macros wich i could call like

    fatal("%d %s", var1, var2);

    and which would be preprocessed into

    printfatal(__FI LE__, __LINE__, "%d %s", var1, var2);

    is that possible?
  • Kevin D. Quitt

    #2
    Re: macros question #2

    On 8 Apr 2004 11:42:41 -0700, arro@arro.ru (Andrew Arro) wrote:[color=blue]
    >how could i define a macros with variable number of parameters?
    >fatal("%d %s", var1, var2);
    >printfatal(__F ILE__, __LINE__, "%d %s", var1, var2);[/color]

    #define fatal( s, ... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )

    Assuming your compiler handles C99.

    --
    #include <standard.discl aimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list

    Comment

    • E. Robert Tisdale

      #3
      Re: macros question #2

      Kevin D. Quitt wrote:
      [color=blue]
      > Andrew Arro wrote:
      >[color=green]
      >>How could I define a macros with variable number of parameters?
      >>fatal("%d %s", var1, var2);
      >>printfatal(__ FILE__, __LINE__, "%d %s", var1, var2);[/color]
      >
      >
      > #define fatal( s, ... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )[/color]

      #define fatal( s, ... ) printfatal( __FILE__, __LINE__, s, __VA_ARGS__ )

      Comment

      • Andrew Arro

        #4
        Re: macros question #2

        "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message news:<4075A9D7. 1080203@jpl.nas a.gov>...[color=blue]
        > Kevin D. Quitt wrote:
        >[color=green]
        > > Andrew Arro wrote:
        > >[color=darkred]
        > >>How could I define a macros with variable number of parameters?
        > >>fatal("%d %s", var1, var2);
        > >>printfatal(__ FILE__, __LINE__, "%d %s", var1, var2);[/color]
        > >
        > >
        > > #define fatal( s, ... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )[/color]
        >
        > #define fatal( s, ... ) printfatal( __FILE__, __LINE__, s, __VA_ARGS__ )[/color]

        thanx guys, actualy the best option i think is

        #define fatal(... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )

        this case
        fatal("message" )

        also works

        thanx again!

        Comment

        • Kevin D. Quitt

          #5
          Re: macros question #2

          On Thu, 08 Apr 2004 12:26:23 -0700, Kevin D. Quitt
          <KQuittUNMUNG@I EEIncUNMUNG.com > wrote:[color=blue]
          >#define fatal( s, ... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )[/color]
          ^^
          I'm not sure where that came from, but it doesn't belong. Sorry.

          --
          #include <standard.discl aimer>
          _
          Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
          Per the FCA, this address may not be added to any commercial mail list

          Comment

          • Dave Thompson

            #6
            Re: macros question #2

            On 8 Apr 2004 11:42:41 -0700, arro@arro.ru (Andrew Arro) wrote:
            [color=blue]
            > how could i define a macros with variable number of parameters? i want
            > to create
            >
            >
            >
            > void printfatal(char *file, int line, char *format, ...);
            >
            >
            > so i wanna define a macros wich i could call like
            >
            > fatal("%d %s", var1, var2);
            >
            > and which would be preprocessed into
            >
            > printfatal(__FI LE__, __LINE__, "%d %s", var1, var2);
            >
            > is that possible?[/color]

            In addition to the answer already given for C99, which isn't yet
            widely implemented, you can do almost but not quite the same thing in
            GCC even in older versions.

            In the absence of either of those, AFAIK the closest you can come is a
            pseudoCurrying hack:

            #define fatal /*NOT functionlike*/ \
            printfileline(_ _FILE__,__LINE_ _), printf /* gets "arguments" */
            /* or instead of printf other appropriate variadic function */

            or
            #define fatal printfilelineth enprintf(__FILE __,__LINE__) which is
            declared to return a function pointer and actually returns a pointer
            to printf or whatever, which then gets invoked the arguments.

            - David.Thompson1 at worldnet.att.ne t

            Comment

            Working...