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.
1. ptr – a pointer to the first element of a block of memory being used as a 5-
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)
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.
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