macro style guideline

Discussion in 'C Programming' started by Vyom, Nov 21, 2004.

  1. Vyom

    Vyom Guest

    I wanted a suggestion regarding the following macro.
    I have to check the existence of some element in a 2D array,

    Which of the following would be the best way to write

    1.) pass the array name, row and col
    #define EXISTS_IN_ARRAY(A,R,C) (A[R][C]=='x')

    2.) pass only the row and col
    #define EXISTS_IN_ARRAY(R,C) (array[R][C]=='x')

    3.) No arguments
    #define EXISTS_IN_ARRAY (array[j]=='x')

    --
    Vyom
    Vyom, Nov 21, 2004
    #1
    1. Advertising

  2. Vyom

    Trent Buck Guest

    Quoth Vyom on or about 2004-11-21:
    > I wanted a suggestion regarding the following macro.
    > I have to check the existence of some element in a 2D array,


    None of the examples you gave will perform bounds checking. I don't
    think bounds checking is possible in C, short of remembering what size
    you allocated in a separate variable (or macro, for statically allocated
    arrays).

    They all compare the contents at a particular address to the character
    'x'. This is probably not what you meant to do.

    The second form will not compile unless `array' is visible in the
    scopes in which EXISTS_IN_ARRAY is called; likewise for `i' and `j' in
    the third form.

    Which of the three examples depends is best depends a lot on how you're
    using the macro, although the second and third forms are bad style (see
    previous paragraph).

    -trent
    Trent Buck, Nov 21, 2004
    #2
    1. Advertising

  3. On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , (Vyom)
    wrote:

    >I wanted a suggestion regarding the following macro.
    >I have to check the existence of some element in a 2D array,
    >
    >Which of the following would be the best way to write


    none of them. Of them all, only the first comes close, and you probably
    want something more like

    #define EXISTS_IN_ARRAY(A,R,C,X) ((A)[(R)][(C)]==(X)

    with extra parens to avoid errors of the MACRO(foo+3,r,c,x) sort, and
    passing in X so you can vary what you look for.

    Personally I'd do this with a function...
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Nov 21, 2004
    #3
  4. On Sun, 21 Nov 2004 15:41:03 +0000, Mark McIntyre wrote:

    > On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , (Vyom)
    > wrote:
    >
    >>I wanted a suggestion regarding the following macro.
    >>I have to check the existence of some element in a 2D array,
    >>
    >>Which of the following would be the best way to write

    >
    > none of them. Of them all, only the first comes close, and you probably
    > want something more like
    >
    > #define EXISTS_IN_ARRAY(A,R,C,X) ((A)[(R)][(C)]==(X)


    You left out a parenthesis, should be

    #define EXISTS_IN_ARRAY(A,R,C,X) ((A)[(R)][(C)]==(X))

    > with extra parens to avoid errors of the MACRO(foo+3,r,c,x) sort, and
    > passing in X so you can vary what you look for.
    >
    > Personally I'd do this with a function...


    Rob Gamble
    Robert Gamble, Nov 21, 2004
    #4
  5. On Sun, 21 Nov 2004, Mark McIntyre wrote:
    >
    > On 21 Nov 2004 06:00:34 -0800, in comp.lang.c, Vyom wrote:
    >> I wanted a suggestion regarding the following macro.
    >> I have to check the existence of some element in a 2D array,
    >> Which of the following would be the best way to write

    >
    > none of them. Of them all, only the first comes close, and you probably
    > want something more like
    >
    > #define EXISTS_IN_ARRAY(A,R,C,X) ((A)[(R)][(C)]==(X)


    (1) As Rob said, you left out a ')'.
    (2) Two of those parenthesis pairs are just noise and need removed.

    #define EXISTS_IN_ARRAY(A,R,C,X) ((A)[R][C] == (X))

    (3) If you're writing that much already, why bother with a macro?
    The place macros are useful is where they either clarify the programmer's
    intent (which this macro doesn't seem to) or let the programmer express
    more with less typing --- which you certainly haven't accomplished here!


    > Personally I'd do this with a function...


    You can't do this with a function, in C. C++ has templates that would
    help somewhat. But really, all you're doing is testing two values for
    equality --- why do you need a function at all?!

    To the OP: Post the relevant code, if you want help with it.

    -Arthur
    Arthur J. O'Dwyer, Nov 22, 2004
    #5
  6. Vyom

    Vyom Guest

    > None of the examples you gave will perform bounds checking. I don't
    > think bounds checking is possible in C, short of remembering what size
    > you allocated in a separate variable (or macro, for statically allocated
    > arrays).


    Acutally, I will be using it in a loop, something like

    for (i = 0 ; i< MAX ;i ++ )
    for (j = 0 ; j< MAX ;j ++ )
    {
    // ...............
    EXISTS_IN_ARRAY(a,i,j);
    // ...............
    }

    So bound checking should not be a problem.
    Also, the macros will be called in one function only.

    > Which of the three examples depends is best depends a lot on how you're
    > using the macro, although the second and third forms are bad style (see
    > previous paragraph).
    >
    > -trent
    Vyom, Nov 22, 2004
    #6
  7. Vyom

    Trent Buck Guest

    Quoth Vyom on or about 2004-11-21:
    > Acutally, I will be using it in a loop, something like
    >
    > for (i = 0 ; i< MAX ;i ++ )
    > for (j = 0 ; j< MAX ;j ++ )
    > {
    > // ...............
    > EXISTS_IN_ARRAY(a,i,j);
    > // ...............
    > }
    >
    > So bound checking should not be a problem.
    > Also, the macros will be called in one function only.


    Then, why do you need a macro at all?

    -t
    Trent Buck, Nov 22, 2004
    #7
  8. Vyom

    Dan Pop Guest

    In <> (Vyom) writes:

    >I wanted a suggestion regarding the following macro.
    >I have to check the existence of some element in a 2D array,
    >
    >Which of the following would be the best way to write
    >
    >1.) pass the array name, row and col
    >#define EXISTS_IN_ARRAY(A,R,C) (A[R][C]=='x')
    >
    >2.) pass only the row and col
    >#define EXISTS_IN_ARRAY(R,C) (array[R][C]=='x')
    >
    >3.) No arguments
    >#define EXISTS_IN_ARRAY (array[j]=='x')


    It's not a matter of style but of what you actually need. Make it as
    general as needed by your application (including its foreseeable future
    versions) but no more general than that.

    If the arguments are *always* the same and there is no good reason
    to assume that this could change in the future, the version with no
    arguments is perfectly OK.

    Otherwise, include in the argument list only the ones that are different
    in different invocations of the macro.

    Another concern is that the usage of the macro should fit within the C
    syntax framework: you don't want to use macros that make your code look
    weird, like:

    if (foo) BAR
    else SWAP

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Nov 23, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno

    style guideline for naming variables?

    John Salerno, Mar 17, 2006, in forum: Python
    Replies:
    2
    Views:
    272
    Duncan Smith
    Mar 18, 2006
  2. lovecreatesbeauty

    Redundant behavior in coding guideline

    lovecreatesbeauty, Oct 27, 2005, in forum: C Programming
    Replies:
    0
    Views:
    402
    lovecreatesbeauty
    Oct 27, 2005
  3. lovecreatesbeauty

    Redundant behavior in coding guideline

    lovecreatesbeauty, Oct 27, 2005, in forum: C Programming
    Replies:
    2
    Views:
    330
    Netocrat
    Oct 27, 2005
  4. lovecreatesbeauty
    Replies:
    17
    Views:
    594
    Jordan Abel
    Jan 1, 2006
  5. Paul McGuire

    Is PEP-8 a Code or More of a Guideline?

    Paul McGuire, May 27, 2007, in forum: Python
    Replies:
    52
    Views:
    1,013
    Ben Finney
    Jun 1, 2007
Loading...

Share This Page