Quantcast
Channel: openSUSE Forums
Viewing all articles
Browse latest Browse all 40713

Test bounds macro in C will not work

$
0
0
Hi, I wrote a test macro to check when I went beyond the bounds of an array vs. writing the same test over and over again but gcc will not accept it. Here's an over simplified version that exhibits the bug.

Code:

#define TESTME(pos, maxpos) ((pos) <= (maxpos)) ? : return(0);

int main(void)
{
    int a = 0;
    int b = 100;
    while(a++)
    {
        TESTME(a, b);
    }
   
    return(0);
}

Code:

% gcc a.c
a.c: In function ‘main’:
a.c:1:53: error: expected expression before ‘return’
 #define TESTME(pos, maxpos) ((pos) <= (maxpos)) ? : return(0);
                                                    ^
a.c:9:9: note: in expansion of macro ‘TESTME’
        TESTME(a, b);
        ^

If I code it with an if test then it will compile. I'm not understanding why the ? : construct does not do the job. To me the ? : construct makes a prettier one liner and even if I were to just decide that the if was ok I still want to know what makes the? : unacceptable as I desire to understand the language better and better.

Viewing all articles
Browse latest Browse all 40713

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>