So I think my professor's starting to run out of ideas for exercises, unless the real exercise here is to learn how weird macros are. He wants us to write one that sums the elements of an array, which I would think is a job for a function! I basically defined the macro as I would a function. I didn't think it was possible to return a specific variable though, so I added a printf() on the end.
I hope someone can bear with me here. I would much rather use a function, but I wouldn't get credit for the assignment if I did! Ugh. Well, here's what I have:
Code:
/*Define and use a macro SUMARRAY to sum the values of a numeric array.*/
#include<stdio.h>
#define SUMARRAY( array[10] ) \
( \
int total = 0; \
int arraySize = sizeof(array[10]) / sizeof(int); \
while ( i <= arraySize) \
{ \
total += array[i]; \
i++; \
} \
printf ( "%d", total ); \
) \
int main()
{
int sum = 0;
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
SUMARRAY( array[10] );
return 0;
}