Go to Post Secure your battery well, or it will fly like a fish. - Nate Laverdure [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 14-02-2004, 13:28
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
Conditional #define Statements

I'm rewriting the navigate.c class for our team, and I've come to an annoying problem.

I have two sensors, left and right, for each side of the robot. Depending on a switch, only one should be read. That means I can save on code, I think.

With PBASIC, a simple if statement and alias command would set "sensor" to "left_sensor" or "right_sensor" but I have no idea how to do this in C, without going into pointers (which I don't remember).

Is there any way I can make a conditional #define (or similar) statement?
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #2   Spotlight this post!  
Unread 14-02-2004, 14:59
Paul Paul is offline
lacks sanity
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Jan 2003
Rookie Year: 2002
Location: Salem,NJ
Posts: 14
Paul is an unknown quantity at this point
Send a message via AIM to Paul
Re: Conditional #define Statements

Why not just have a conditional declaration?
if(your condition is meet)
{
int your_var;
}
else
{
int something else;
}

I'm probably misunderstanding you but its still worth a shot.
  #3   Spotlight this post!  
Unread 14-02-2004, 15:19
steven114 steven114 is offline
Programming Wizard and Team Captain
AKA: Steven Schlansker
FRC #0114 (Eaglestrike)
Team Role: Programmer
 
Join Date: Feb 2004
Location: Los Altos, CA
Posts: 335
steven114 is a jewel in the roughsteven114 is a jewel in the roughsteven114 is a jewel in the rough
Send a message via AIM to steven114
Re: Conditional #define Statements

Quote:
Originally Posted by Paul
Why not just have a conditional declaration?
if(your condition is meet)
{
int your_var;
}
else
{
int something else;
}

I'm probably misunderstanding you but its still worth a shot.
Won't work. The declaration goes out of scope after the if block. There are two methods -

1) You know at compile time which one you want
Code:
#if IWANTVAR1
#define myVar var1
#else
#define myVar var2
#endif

//myVar now refers to var1 or var2, depending on IWANTVAR1 (which MUST be either a constant or expand to a constant)
2) You know at runtime

Code:
int myVar;
if(iWantVar1)
myVar = var1;
else
myVar = var2;
Note that way 2 only works for reading the variable, not writing. If you want something like the aliases, you either must know at compile time or deal with pointers (afaik)

I hope this is what you wanted...
  #4   Spotlight this post!  
Unread 14-02-2004, 18:14
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
Re: Conditional #define Statements

Ah, thanks, I didn't think about doing something simple like just reassigning the variable to a temporary variable. Seeing as I only need to read, I think I can make do with that, but I'll have to fool around with it a bit.

On the other hand, it's always a good time to relearn pointers...
__________________
AIM --> Woloi
Email --> ian@woloschin.com
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Contradictory FIRST Statements Gobiner Rules/Strategy 10 14-01-2003 16:12
Calling all Lawyers... ...Define "all parts" Joe Johnson General Forum 10 13-03-2002 15:12
Logical statements or math formulas Manoel Programming 1 16-02-2002 23:29


All times are GMT -5. The time now is 04:24.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi