preprocessor question

C

cerr

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
 
H

Hallvard B Furuseth

cerr said:
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.
 
K

Keith Thompson

cerr said:
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.
 
J

John Bode

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.
 
C

cerr

cerr said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top