|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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? |
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
Re: Conditional #define Statements
Quote:
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) Code:
int myVar; if(iWantVar1) myVar = var1; else myVar = var2; I hope this is what you wanted... |
|
#4
|
||||
|
||||
|
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... |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Contradictory FIRST Statements | Gobiner | Rules/Strategy | 10 | 01-14-2003 04:12 PM |
| Calling all Lawyers... ...Define "all parts" | Joe Johnson | General Forum | 10 | 03-13-2002 03:12 PM |
| Logical statements or math formulas | Manoel | Programming | 1 | 02-16-2002 11:29 PM |