Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > preprocessor question

Reply
Thread Tools

preprocessor question

 
 
cerr
Guest
Posts: n/a
 
      06-30-2009
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???
This is puzzling me...

Thanks,
Ron
 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      06-30-2009
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???


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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-30-2009
cerr <(E-Mail Removed)> 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???


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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      06-30-2009
On Jun 30, 12:40*pm, cerr <(E-Mail Removed)> 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???
> This is puzzling me...
>
> 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.
 
Reply With Quote
 
cerr
Guest
Posts: n/a
 
      06-30-2009
On Jun 30, 11:07*am, Hallvard B Furuseth <(E-Mail Removed)>
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???

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
a question about preprocessor xuatla C++ 1 07-10-2004 11:03 AM
preprocessor implementation GURU question Dan W. C++ 9 12-04-2003 02:24 AM
Preprocessor's question C Programming 1 09-12-2003 05:32 PM



Advertisments