MACROS in C not C++

I have this homework, if you think I am asking you to answer my homework and you are uncomfortable with it, feel free to ignore this request:

I am to use C not C++, I need to write a macro that will work for any type of variable.


Below is a section of the program provided for by my instructor.
The macro string is " (m5D((double *)bar, ix0, ix1, ix2, ix3, ix4, D_B, D_C,D_D, D_E) and (m5D((int *)foo, ix0, ix1, ix2, ix3, ix4, D_B, D_C,D_D, D_E)
note: one is type cast as (int*) and the other is type cast as (double*). I must only write one MACRO not one for type int and another for type double.

Parameters:
1. ptr – a pointer to the first element of a block of memory being used as a 5-
dimensional array
2. idx0, idx1, idx2, idx3, idx4 – indices of the desired element of the array

3. dim1, dim2, dim3, dim4 – the rightmost 4 dimensions of the 5-dimensional
array (no dim0)

for (ix4 = 0; ix4 < D_E; ++ix4)
{
if (m5D((int *)foo, ix0, ix1, ix2, ix3, ix4, D_B, D_C,
D_D, D_E) != foo[ix0][ix1][ix2][ix3][ix4])
{
fprintf(stderr, "Error: foo[%d][%d][%d][%d][%d]\n",
ix0, ix1, ix2, ix3, ix4);
return EXIT_FAILURE;
}
if (m5D((double *)bar, ix0, ix1, ix2, ix3, ix4, D_B, D_C,
D_D, D_E) != bar[ix0][ix1][ix2][ix3][ix4])
{
fprintf(stderr, "Error: bar[%d][%d][%d][%d][%d]\n",
ix0, ix1, ix2, ix3, ix4);
return EXIT_FAILURE;
}
}




I created this macro using the #define, but the (type*) inside the MACRO is causing a compiler error.

The (type*) is needed to typecast the result of the MACRO.

The way I understood MACRO is just a plain string substitution, if I remove the type cast (int*) or (double*) the compiler will not match any string because it is in the middle of the string . Therefore nothing will be replaced. Please tell me if I understood MACRO wrong. I search the web but it appears that how I learned it is correct.

How do I deal with it?, do I do it piece by piece?


THIS IS THE MACRO I CREATED: I am to write a linear procedure to locate the Array data in memory, not using the Index method which is a for-loop.

#define m5D(type*)(ptr, idx0, idx1, idx2, idx3, idx4, dim1, dim2, dim3, dim4) \

(type*)(ptr + (idx0 * dim1 * dim2 * dim3 * dim4) + (idx1 * dim2 * dim3 * dim4) \
+ (idx2 * dim3 * dim4) + (idx3 * dim4) + (idx4)))


Ed
You've got really stupid requirements.
Just don't put the type in the macro, let the user write it.
1
2
3
#define m5D(casted_ptr, idx0, idx1, idx2, idx3, idx4, dim1, dim2, dim3, dim4) \
( (casted_ptr) + (idx0 * dim1 * dim2 * dim3 * dim4) + (idx1 * dim2 * dim3 * dim4) \
+ (idx2 * dim3 * dim4) + (idx3 * dim4) + (idx4))) 
didn't check for correctness.
Also you need to parenthesize all the parameters to avoid weird expansions (which I didn't because I'm lazy)

I think that the index and dimension would be better as arrays
ne555,

I would assume you are telling me that (int*)ptr should be treated as one parameter.
I will try this, and see if the compiler will accept it.

The requirements may look stupid, by my instructor has a reason.
1. If the requirement is just to write a program, then I could do the entire program in an hour because the assignment is just to compare data taken by linear method to the one taken by index method.

2. The Class is UCSD - C/C++ Programming, and there are times he is very specific, he wants the program written in C and not C++. He wants to be sure we do not mix C and C++ instructions in one program.

3. There are times he wants us to use MACRO or inline Function when it could be done by writting the code directly in the main(). The instructor is very good and he is also employed by the US Navy SPAWAR, so he knows theory and experience. He will tell you a lot of approach to a program and he will tell you reasons why use the an approach versus the other

Anyway thanks for the feedback.

ne555,

It did not work, still having compiler errors.

Well, it would help if you tell us the errors.
You could also take a look at `translated' code (the preprocessor output), it's the -E flag for gcc.
Topic archived. No new replies allowed.