Beware the over-use of macros. Macro bugs are insidious and difficult to find. Our emphasis in this course will be simple object and function macros.
We will now experiment a bit with macros and see exactly what happens in some of the common uses of macro expressions. Assume the int is no more than 32 bits wide. This program takes 2 ints on the command line. Those ints represent a range of values to be examined. For every value in that range, a line of output is echoed containing the value itself and the BINARY representation of the value after it.
Below is a sample snapshot of a run:
bash-3.00$ clear bash-3.00$ ./a.out usage: ./a.out <low> <hi> bash-3.00$ ./a.out 0 25 binary value of 0: 00000000000000000000000000000000 binary value of 1: 00000000000000000000000000000001 binary value of 2: 00000000000000000000000000000010 binary value of 3: 00000000000000000000000000000011 binary value of 4: 00000000000000000000000000000100 binary value of 5: 00000000000000000000000000000101 binary value of 6: 00000000000000000000000000000110 binary value of 7: 00000000000000000000000000000111 binary value of 8: 00000000000000000000000000001000 binary value of 9: 00000000000000000000000000001001 binary value of 10: 00000000000000000000000000001010 binary value of 11: 00000000000000000000000000001011 binary value of 12: 00000000000000000000000000001100 binary value of 13: 00000000000000000000000000001101 binary value of 14: 00000000000000000000000000001110 binary value of 15: 00000000000000000000000000001111 binary value of 16: 00000000000000000000000000010000 binary value of 17: 00000000000000000000000000010001 binary value of 18: 00000000000000000000000000010010 binary value of 19: 00000000000000000000000000010011 binary value of 20: 00000000000000000000000000010100 binary value of 21: 00000000000000000000000000010101 binary value of 22: 00000000000000000000000000010110 binary value of 23: 00000000000000000000000000010111 binary value of 24: 00000000000000000000000000011000 binary value of 25: 00000000000000000000000000011001 bash-3.00$
This program takes the same 2 ints on the command line and produces the exact same output. This time however the print loop to echo the bits of the value is moved to a macro. This macro calls the ithBit macro in its loop. Thus the nesting of the macros.
Function pointers are not so different to regular pointers - just some new syntax
A VARIABLE | EXPR PRODUCING ADDR OF VAR | VAR OF TYPE: PTR TO THAT VAR | PTR GETS ADDR OF VAR |
int x; |
&x |
int * ptr; |
ptr = &x; |
....... THE FUNCTION VERSION ......... | |||
void myPrint(); /* prototype for my little print function */ |
myPrint /* funct name w/out () is the addr of the function */ |
void (*print)(); /* a pointer var named "print". type is: ptr to a func that returns void and takes no args */ |
print = myPrint; /* ptr var named print gets addr of an actual function that returns void and takes no args |