#define Question/Standard ?

This is a discussion on #define Question/Standard ? within the C Programming forums, part of the General Programming Boards category; Hello all ... i was just wondering if this migth be a rigth/wrong perhaps bad practice/pron to errors way to ...

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    #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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    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

    ps...Bad spelling and grammar is my weakness, please bare with me

  4. #4
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    <--- C of Green's post above

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >#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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  3. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21