Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   preprocessor question (http://www.velocityreviews.com/forums/t689642-preprocessor-question.html)

cerr 06-30-2009 05:40 PM

preprocessor question
 
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??? :o
This is puzzling me... :o

Thanks,
Ron

Hallvard B Furuseth 06-30-2009 06:07 PM

Re: preprocessor question
 
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??? :o


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

Keith Thompson 06-30-2009 06:12 PM

Re: preprocessor question
 
cerr <ron.eggler@gmail.com> 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??? :o


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) kst-u@mib.org <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"

John Bode 06-30-2009 07:20 PM

Re: preprocessor question
 
On Jun 30, 12:40*pm, cerr <ron.egg...@gmail.com> 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??? :o
> This is puzzling me... :o
>
> 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.

cerr 06-30-2009 08:12 PM

Re: preprocessor question
 
On Jun 30, 11:07*am, Hallvard B Furuseth <h.b.furus...@usit.uio.no>
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??? :o

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




All times are GMT. The time now is 03:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.