How to compare value of variable against #define - objective-c

I have made the following #defines
#define GOLD 1;
#define SILVER 2;
later in my program I set up some variables
int source, target;
They are then set to the values:
source = GOLD;
target = SILVER;
I now want to compare in an if statement:
if(source == GOLD) {...}
But the compiler complains Expected ')' before ';' token. If I change the line to
if(source == 1) {...}
There is no problem with the compiler or the behavior. How do I reference the #define in the if statement to make myself clear to the compiler?

Because you have an errant ; after the #define GOLD 1 and #define SILVER 2. Remove them, your problem will go away.

Related

Why does my usb HID output rubbish? STM32Cube

I'm trying to make a force feedback wheel, but software isn't my cup of tea.
This is supposed to toggle button 0 and it doesn't.
typedef struct{
uint8_t buttons;
int8_t relativeMvt;
}steer_t;
steer_t steer = {0, 0};
while (1)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
if(steer.buttons) steer.buttons = 0b00000000;
else steer.buttons = 0b00000001;
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, steer, sizeof(steer));
HAL_Delay(500);
}
My Report descriptor (this is the first time I'm using one):
Running that code, the buttons are static "ON" like so:
They DO change (randomly) only when the "relativeMvt" variable is changed, very weird.
What I've tried:
Swap relativeMvt and buttons in the typeDef
Check the report descriptor size etc
Cry
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 2
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 45
#define CUSTOM_HID_EPIN_SIZE 2
What do I have to change to make it work? Thanks!
I've solved it. I was missing:
#include "usbd_customhid.h"
and I forgot the "&" when passing my variables:
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &steer, sizeof(steer));

'unsigned short' array's length in Objective C

In Objective C (iOS app), I have
unsigned short Alpha[a][b][c]; //this is a global array initialized
//somewhere else. Here, let a=5, b=6, c=7
Now in a function I want the length of the array for predefined a and b.
I tried the following:
[Alpha[1][2] length]; //unfortunately no such method exists
[Alpha[1][2] count]; //Warning: Invalid receiver type 'short unsigned int [7]'
So I am left with (I guess):
(sizeof (Alpha[1][2])) / (sizeof (Alpha[1][2][0]))
But as this is a global array, so the function will only get a reference to it.
So, this means it won't give me the desired result or am I wrong?
Any help on this or any other way of doing it would be much appreciated.
There is implementation of macro _countof
#if !defined(_countof)
#if !defined(__cplusplus)
#define _countof(_Array) (sizeof(_Array) / sizeof(0[_Array]))
#else
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
#endif
#endif
Size values will be:
a = _countof(Alpha);
b = _countof(0[Alpha]);
c = _countof(0[Alpha][0]);
If you use it in .mm then will be used more secure C++ version.

How to test if defined macro is empty at compile time (Cbjective-C / c)?

Is it possible to do check if I've got an empty define? IS_EMPTY_OR_UNDEFINED is a fictive macro I just came up with.
#define constantA 0
#define constantB 1
#define constantC null
#define constantF ""
#define constantH
#if IS_EMPTY_OR_UNDEFINED(constantA)
# error constantA is defined 0 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantB)
# error constantB is defined 1 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantC)
# error constantC is defined null and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantF)
# error constantF is defined "" and the above if should not be true - this line should not run
#endif
#if ! IS_EMPTY_OR_UNDEFINED(constantH)
# error constantH is defined empty and the above if should not be true - this line should not run
#endif
#if defined(undefinedConstant) && ! IS_EMPTY_OR_UNDEFINED(undefinedConstant)
# error undefinedConstant is not defined and the above if should not be true - this line should not run
#endif
Checking if an expression is empty can be done (modulo some really exotic border case) but the technique is somewhat complicated. P99 has a macro that does this and which you could be using as
#if !defined(constantA) || P99_IS_EMPTY(constantA)
...
#endif
Combining this in one single macro is not allowed by the C standard. AS of C11 6.10.1 p4, the token defined is not allowed inside macros or other expressions.
You can use #ifdef MACRO or #if defined(MACRO) to test if macros are defined at all. You can also compare them, although only against integers:
#if MACRO > 5
etc. Maybe this helps you?
Edit: Although I don't know how you would check if a definition evaluates to "empty", or if this is possible.

In Objective-C, how to test against macro definition value?

I'd like to use
#define IS_APP_FULL_VERSION NO
and the code
isAppFullVersion = IS_APP_FULL_VERSION;
to set instance variable. But is there a way to also do
#if IS_APP_FULL_VERSION == "NO"
// add some methods here
#endif
but it would give a compile error, and so is #if (IS_APP_FULL_VERSION == "NO"). Is there a way to check it against YES and NO? (check against the substitution value)
Update: it seems like > and == 0 is allowed, such as
#if IS_APP_FULL_VERSION == 0
so we can actually use 0 and 1 for false and true, and use == 1 to test, but it will be better if YES and NO can be used.
Update 2:
One possible solution turns out to be:
#define IS_APP_FULL_VERSION 1
#if IS_APP_FULL_VERSION
// add some methods here
#endif
isAppFullVersion = IS_APP_FULL_VERSION;
will all work, and we can just change 1 to 0 to toggle the code.
Your basic problem is that the macro processor #if statement only has one data type -- integer. All #if expressions are evaluated in integer, with undefined symbols being replaced with zero. String expressions (including comparisons) cannot be evaluated.
Actually, normally, I would just use a flag macro rather than a value one, since there's only two cases here:
#define APP_IS_FULL_VERSION
Either define or don't define that macro. Then, if you later wanted to include/exclude code based on that, you can use:
#ifdef APP_IS_FULL_VERSION
// Do something
#endif
(or #ifndef for the opposite case, obviously).
For the code segment, it's a simple matter of using that to select the correct code:
#ifdef APP_IS_FULL_VERSION
isAppFullVersion = YES;
#else
isAppFullVersion = NO;
#endif
I think, you're some confused comparison MACRO.
#define format is following it.
#define MACRO_NAME VALUE (VALUE is blank spaces are also fine. MACRO_NAME need is.)
if you want comparison compile-time following like it. this is comparison mean only defined vs no defined. not YES vs NO
#define IS_APP_FULL_VERSION 1
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
#define IS_APP_FULL_VERSION blahblah
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
If you want to build separate versions of. refer a following like it.
#define IS_APP_FREE_VERSION
#if defined IS_APP_FREE_VERSION
NSLog(#"free-version");
#endif
#define IS_APP_FULL_VERSION
#if defined IS_APP_FULL_VERISON
NSLog(#"full-version");
#endif
if you want comparison runtime following like it. this is comparsion normally.
#define IS_APP_FULL_VERSION 1
if(IS_APP_FULL_VERSION)
{
NSLog(#"full-verison");
}
#define IS_APP_FULL_VERSION 0
if(!IS_APP_FULL_VERSION)
{
NSLog(#"no full-version");
}

Checking if it is equal, Normal int and #define macro. but it doesn't work

I have an iVar named,
int DATA_IN_TRANSIT;
and I have defined several macros, e.g.
#define PLACES 0;
When I do something like the following,
if(DATA_IN_TRANSIT == PLACES)
{
NSLog(#"Make LLVM Dance!");
}
I get a compiler error (expression expected) in the line if(DATA_IN_TRANSIT == PLACES)
I don't know why it's giving me an error? Am I doing something naive?
#define PLACES 0
but without ';'
otherwise you'll get
if(DATA_IN_TRANSIT == 0;)
{
NSLog(#"Make LLVM Dance!");
}