preprocessor question

Discussion in 'C Programming' started by cerr, Jun 30, 2009.

  1. cerr

    cerr Guest

    Hi There,

    In my code i would like to use the following:
    #ifdef PACE
    unsigned char pin = atoi(app_tran[direction])+4; // we need pins 5-8
    for PACE, direction 0-3
    #else
    unsigned char pin = atoi(app_tran[direction]); // we generically
    need pins 1-4 for direction 0-3
    #endif
    I'm compiling this using gcc but if i define PACE or if I don't, I
    always get
    "'pin' undeclared (first use in this function)' at a later access
    point, why that??? :eek:
    This is puzzling me... :eek:

    Thanks,
    Ron
    cerr, Jun 30, 2009
    #1
    1. Advertising

  2. cerr writes:
    > In my code i would like to use the following:
    > #ifdef PACE
    > unsigned char pin = atoi(app_tran[direction])+4; // we need pins 5-8
    > for PACE, direction 0-3
    > #else
    > unsigned char pin = atoi(app_tran[direction]); // we generically
    > need pins 1-4 for direction 0-3
    > #endif
    > I'm compiling this using gcc but if i define PACE or if I don't, I
    > always get "'pin' undeclared (first use in this function)' at a later
    > access point, why that??? :eek:


    PACE seems irrelevant. Check the surrounding code. Maybe the 'pin'
    declarataion is inside braces {} and the use is outside it.
    (Missing/extra braces so braces don't match indentation, maybe?) Or
    there is a '/*' above the 'pin' declaration with no closing '*/', and a
    later /* ... */ closes the comment. Or there is an #if around it.
    Or...

    Anyway, maybe gcc -Wall, or if that's not to noisy, gcc -Wall -Wextra,
    will give you a better hint.

    --
    Hallvard
    Hallvard B Furuseth, Jun 30, 2009
    #2
    1. Advertising

  3. cerr <> writes:
    > In my code i would like to use the following:
    > #ifdef PACE
    > unsigned char pin = atoi(app_tran[direction])+4; // we need pins 5-8
    > for PACE, direction 0-3
    > #else
    > unsigned char pin = atoi(app_tran[direction]); // we generically
    > need pins 1-4 for direction 0-3
    > #endif
    > I'm compiling this using gcc but if i define PACE or if I don't, I
    > always get
    > "'pin' undeclared (first use in this function)' at a later access
    > point, why that??? :eek:


    You have a typo on line 42.

    If you want a better answer than that, you'll need to show us some
    actual code, including the line gcc is complaining about.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 30, 2009
    #3
  4. cerr

    John Bode Guest

    On Jun 30, 12:40 pm, cerr <> wrote:
    > Hi There,
    >
    > In my code i would like to use the following:
    > #ifdef PACE
    >         unsigned char pin = atoi(app_tran[direction])+4; // we need pins 5-8
    > for PACE, direction 0-3
    > #else
    >         unsigned char pin = atoi(app_tran[direction]);   // we generically
    > need pins 1-4 for direction 0-3
    > #endif
    > I'm compiling this using gcc but if i define PACE or if I don't, I
    > always get
    > "'pin' undeclared (first use in this function)' at a later access
    > point, why that??? :eek:
    > This is puzzling me... :eek:
    >
    > Thanks,
    > Ron


    It sounds like you're defining pin in a different scope from where
    it's being used later; something like:

    if (foo)
    {
    #ifdef PACE
    unsigned char pin = ...
    #else
    unsigned char pin = ...
    }
    ....
    pin = ...

    In this case, the definition of pin is limited to the scope defined by
    the if statement, which would be a problem regardless of the #ifdef.
    Or, as someone mentioned above, the block containing the #ifdef may be
    disabled by a comment or #if 0.

    Personally, I'd rewrite your snippet as something like:

    #ifdef PACE
    #define OFFSET 4
    #else
    #define OFFSET 0
    #endif
    ....
    unsigned char pin = atoi(app_tran[direction])+OFFSET
    ....

    since the only thing that varies between the two versions is the value
    of the offset. But that's just me.
    John Bode, Jun 30, 2009
    #4
  5. cerr

    cerr Guest

    On Jun 30, 11:07 am, Hallvard B Furuseth <>
    wrote:
    > cerr writes:
    > > In my code i would like to use the following:
    > > #ifdef PACE
    > >    unsigned char pin = atoi(app_tran[direction])+4; // we need pins 5-8
    > > for PACE, direction 0-3
    > > #else
    > >    unsigned char pin = atoi(app_tran[direction]);   // we generically
    > > need pins 1-4 for direction 0-3
    > > #endif
    > > I'm compiling this using gcc but if i define PACE or if I don't, I
    > > always get "'pin' undeclared (first use in this function)' at a later
    > > access point, why that??? :eek:

    >
    > PACE seems irrelevant.  Check the surrounding code.  Maybe the 'pin'
    > declarataion is inside braces {} and the use is outside it.


    Exactly that was the problem... didn't pay enough attention before i
    had my lunch break ;)
    Thabnks for everyone's response tho!
    This has been resolved!

    --
    Ron

    > (Missing/extra braces so braces don't match indentation, maybe?)  Or
    > there is a '/*' above the 'pin' declaration with no closing '*/', and a
    > later /* ... */ closes the comment.  Or there is an #if around it.
    > Or...
    >
    > Anyway, maybe gcc -Wall, or if that's not to noisy, gcc -Wall -Wextra,
    > will give you a better hint.
    >
    > --
    > Hallvard
    cerr, Jun 30, 2009
    #5
    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. Dan W.
    Replies:
    9
    Views:
    331
    Paul Mensonides
    Dec 4, 2003
  2. xuatla
    Replies:
    1
    Views:
    366
    Denis Remezov
    Jul 10, 2004
  3. Cronus
    Replies:
    1
    Views:
    658
    Paul Mensonides
    Jul 15, 2004
  4. ³á³á³á³á

    Preprocessor's question

    ³á³á³á³á, Sep 12, 2003, in forum: C Programming
    Replies:
    1
    Views:
    331
    Martin Ambuhl
    Sep 12, 2003
  5. Neil Zanella

    preprocessor question

    Neil Zanella, Nov 6, 2003, in forum: C Programming
    Replies:
    4
    Views:
    318
    Eric Sosman
    Nov 6, 2003
Loading...

Share This Page