OK, thanks everyone for the help. So you're saying that I need to write it out like I would for a function, which I did (just for practice). See below:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int SUMARRAY(int y[], int size);
int main(void)
{
int x[] = {1, 2, 3, 4};
int sum;
sum = SUMARRAY(x, SIZE);
printf("%d\n", sum);
system("pause");
return 0;
}
int SUMARRAY(int y[], int size)
{
int i;
int total = 0;
for(i = (SIZE - 1); i >= 0; i--){
total = total + y[i];
}
return total;
}
And what I thought all of you said was to write the function out like I normally would, so this is what I wrote:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SUMARRAY(x, y) \
int i;\
int total = 0;\
for(i = (y - 1); i >= 0; i--){\
total = total + x[i];\
}\
return total;\
}
int main(void)
{
int x[] = {1, 2, 3, 4};
int sum;
sum = SUMARRAY(x, 4);
printf("%d\n", sum);
system("pause");
return 0;
}
Only, I keep getting the same errors:
c_13.10.c(17) : error C2059: syntax error : 'type'
c_13.10.c(17) : error C2143: syntax error : missing ';' before 'type'
c_13.10.c(17) : error C2065: 'i' : undeclared identifier
c_13.10.c(17) : error C2065: 'i' : undeclared identifier
c_13.10.c(17) : error C2065: 'i' : undeclared identifier
c_13.10.c(17) : error C2065: 'total' : undeclared identifier
c_13.10.c(17) : error C2065: 'total' : undeclared identifier
c_13.10.c(17) : error C2065: 'i' : undeclared identifier
c_13.10.c(17) : error C2065: 'total' : undeclared identifier
c_13.10.c(19) : error C2143: syntax error : missing ')' before 'string'
c_13.10.c(19) : error C2143: syntax error : missing '{' before 'string'
c_13.10.c(19) : error C2059: syntax error : '<Unknown>'
c_13.10.c(19) : error C2059: syntax error : ')'
c_13.10.c(21) : error C2143: syntax error : missing ')' before 'string'
c_13.10.c(21) : error C2143: syntax error : missing '{' before 'string'
c_13.10.c(21) : error C2059: syntax error : '<Unknown>'
c_13.10.c(21) : error C2059: syntax error : ')'
c_13.10.c(23) : error C2059: syntax error : 'return'
c_13.10.c(25) : error C2059: syntax error : '}
Does this have something to do with the VB C++ compiler?
Is it not possible to declare variables in a macro? If not, then I'm assuming that you declare them in the main body, and then pass them to the macro.
So then I tried declaring the variables in the main body, like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SUMARRAY(x, y, z, t) \
for(z = (y - 1); z >= 0; z--){\
t = t + x[z];\
}\
return t;\
}
int main(void)
{
int x[] = {1, 2, 3, 4};
int i = 0;
int total = 0;
int sum;
sum = SUMARRAY(x, 4, i, total);
printf("%d\n", sum);
system("pause");
return 0;
}
It still doesn't work.