#define problem

Discussion in 'C++' started by Rayn, Apr 26, 2007.

  1. Rayn

    Rayn Guest

    Hi All,

    I am defining as follows,

    #define REIN (a) if(a == NULL) return -1;

    when I try to use this thing in my function like ..

    void abc(char* aName)
    {
    REIN(aName);
    }

    It gives me a compiler error .. Undefined Identifier "a" ..

    what can be the problem?

    thannks in Advance.

    ...
    KiraN Puranik.
    Rayn, Apr 26, 2007
    #1
    1. Advertising

  2. Rayn

    Sumit Rajan Guest

    Rayn wrote:
    > Hi All,
    >
    > I am defining as follows,
    >
    > #define REIN (a) if(a == NULL) return -1;


    You probably want to change that to:
    #define REIN(a) if(a == NULL) return -1

    (Notice the space that I have removed just after "REIN" and before "(a)")

    >
    > when I try to use this thing in my function like ..
    >
    > void abc(char* aName)
    > {
    > REIN(aName);
    > }


    Now (assuming you have made the change stated above) you have a
    completely different issue. You have stated above that abc() is *void*
    function. However, when you expand the macro REIN, you get:

    void abc(char* aName)
    {
    if(aName == 0) return -1;
    }

    You're attempting to return a value from a void function.

    Regards,
    Sumit.
    --
    Sumit Rajan <>
    Sumit Rajan, Apr 26, 2007
    #2
    1. Advertising

  3. Rayn

    Sumit Rajan Guest

    Sumit Rajan, Apr 26, 2007
    #3
  4. Rayn

    Ian Collins Guest

    Rayn wrote:
    > Hi All,
    >
    > I am defining as follows,
    >
    > #define REIN (a) if(a == NULL) return -1;
    >

    Don't. Use an inline function.

    --
    Ian Collins.
    Ian Collins, Apr 26, 2007
    #4
  5. * Ian Collins:
    > Rayn wrote:
    >> Hi All,
    >>
    >> I am defining as follows,
    >>
    >> #define REIN (a) if(a == NULL) return -1;
    >>

    > Don't. Use an inline function.


    Not that I'm disagreeing with the general sentiment, but using an inline
    function wouldn't work here, would it? Or how did you plan to use an inline
    function that's capable of aborting the calling function?

    OP might want to use

    #define REIN(a) do { if (!(a)) return -1; } while(false)

    although I wouldn't use it either, as there's little benefit over just
    using the if-statement directly.

    --
    Martijn van Buul -
    Martijn van Buul, Apr 26, 2007
    #5
  6. Rayn

    Ron Natalie Guest

    Ian Collins wrote:
    > Rayn wrote:
    >> Hi All,
    >>
    >> I am defining as follows,
    >>
    >> #define REIN (a) if(a == NULL) return -1;
    >>

    > Don't. Use an inline function.
    >


    And just how is an inline function going to do the control construct
    he has above?
    Ron Natalie, Apr 26, 2007
    #6
  7. Rayn

    siddhu Guest

    On Apr 26, 5:04 am, Ian Collins <> wrote:
    > Rayn wrote:
    > > Hi All,

    >
    > > I am defining as follows,

    >
    > > #define REIN (a) if(a == NULL) return -1;




    You can use Inline functions.

    But for this case you need to remove space between REIN and (a) in the
    macro.
    And do not return -1 if your function has void as return value.
    siddhu, Apr 26, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prettysmurfed
    Replies:
    3
    Views:
    10,277
    MPBroida
    Oct 24, 2003
  2. Andreas

    #define problem

    Andreas, Oct 30, 2003, in forum: C Programming
    Replies:
    2
    Views:
    361
    Andreas
    Oct 31, 2003
  3. theotyflos
    Replies:
    3
    Views:
    450
    Thomas Matthews
    Feb 19, 2004
  4. robin liu
    Replies:
    3
    Views:
    810
    Robin Liu
    Apr 21, 2006
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    446
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page