1. ## #define Question/Standard ?

Hello all ... i was just wondering if this migth be a rigth/wrong
perhaps bad practice/pron to errors way to use a #define macro.
i have this program that scans/calculates arrays in five different
functions. They all use the same for loop because the arrays are
all the same size. So instead of having the for loop in each function,
i would define it as a mac,

#define LOOP for(count = 0;count < MAXSIZE;count++)

and use it in a function,

int test(int *array1, int *array2)
{
int count;

LOOP /*array1 loop*/
{
/* code */
}

LOOP /*array2 loop*/
{
/* code */
}

return 0;
}

could this cause problems ???

2. You can do it, but it obfuscates the code and actually makes it harder to read. #define is best used for simple constants such as
#define MAX 10

It's easy to understand and follow and serves an important purpose, you don't have to use magic numbers in your code so maintenance is considerably easier.

This is a matter of style, but changing the look of the language with #define has many times proven to solve one problem and create two or three more.

-Prelude

3. ## Thanks for the replies

Prelude ... thanks for the input on this and the Bubble sort post.

I have now seen the changes to the sort function other then
the ("int *fment = array;") and ("int *fment = &array[0];" ).
They are the same. Plus I really understand your idea of saying
"Using code to describe your intentions is always a good idea".
Cause if the array in the above code was called anything other then "array" i could see when confusion could happen.

On the outter loop (taking out the -1), now see why. Cause the
first pointer was pointing to index zero (no MAXSIZE -1) needed, for the outer loop. The Second array had index one. Hence the (MAXSIZE -1) for the inner for loop. The loop was causing the
the pointers to go "out of range".

Still being a newbie and tring to learn, i think i will have to
try and absorb pointers more fully.

Thanks again ... tell my next post, hehe

4. <--- C of Green's post above

5. >#define LOOP for(count = 0;count < MAXSIZE;count++)

In some situations it is handy to have a macro, but a macro should be a general thing. Your macro requires that a variable count and a constant MAXSIZE is present. You could solve such things by using parameters within a macro. Such like:

#define SMALLEST(x,y) ((x < y) ? x : y)

Which you can use as:

z = SMALLEST (x, y);

Note that the parameters of a macro don't have a type, so you can use them in several situations.

Take very good care of the '(' and ')'. If you don't encapsulate a macro with '(' and ')', then easily mistakes can be introduced.

Beside the nice sides of macros the disadvantage is that it is easy to use too much not necessary macros. Also sometimes I see macros with very cryptic names, so that I need to search the macro to see what it actually does.

So before introducing a macro think good about the fact if a macro is really necessary.