Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop condition and #define

I have an bin data table, for sample purposes:

static const unsigned char test_bin[28800] = {0};

#define TEST_DATA_COLUMNS 2
#define TEST_DATA_ROWS (sizeof(test_bin)/sizeof(float)/TEST_DATA_COLUMNS);    
static const float (* test_data)[TEST_DATA_COLUMNS] = (const float (*)[TEST_DATA_COLUMNS]) test_bin;

This fails with error: expected expression before ';' token:

for(size_t i = 0; i < TEST_DATA_ROWS ; i++);

But this works

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

size_t nrows = TEST_DATA_ROWS;
for(size_t i = 0; i < nrows ; i++);

Why?

godbot for quick testing

>Solution :

The problem is the trailing semi-colon at the end of the define for TEST_DATA_ROWS

#define TEST_DATA_ROWS (sizeof(test_bin)/sizeof(float)/TEST_DATA_COLUMNS);
                                                                         ^

This means that the preprocessor generates the following for your loop

for(size_t i = 0; i < (sizeof(test_bin)/sizeof(float)/TEST_DATA_COLUMNS); ; i++)
                                                                        ^ ^

Removing the ; from the end of the define would fix things.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading