macro style guideline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vyom

    #1

    macro style guideline

    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[i][j]=='x')

    --
    Vyom
  • Trent Buck

    #2
    Re: macro style guideline

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

    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

    Comment

    • Mark McIntyre

      #3
      Re: macro style guideline

      On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , vyom2k4@yahoo.c om (Vyom)
      wrote:
      [color=blue]
      >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[/color]

      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 =----

      Comment

      • Robert Gamble

        #4
        Re: macro style guideline

        On Sun, 21 Nov 2004 15:41:03 +0000, Mark McIntyre wrote:
        [color=blue]
        > On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , vyom2k4@yahoo.c om (Vyom)
        > wrote:
        >[color=green]
        >>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[/color]
        >
        > 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)[/color]

        You left out a parenthesis, should be

        #define EXISTS_IN_ARRAY (A,R,C,X) ((A)[(R)][(C)]==(X))
        [color=blue]
        > 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...[/color]

        Rob Gamble

        Comment

        • Arthur J. O'Dwyer

          #5
          Re: macro style guideline


          On Sun, 21 Nov 2004, Mark McIntyre wrote:[color=blue]
          >
          > On 21 Nov 2004 06:00:34 -0800, in comp.lang.c, Vyom wrote:[color=green]
          >> 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[/color]
          >
          > 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)[/color]

          (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!

          [color=blue]
          > Personally I'd do this with a function...[/color]

          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

          Comment

          • Vyom

            #6
            Re: macro style guideline

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

            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.
            [color=blue]
            > 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[/color]

            Comment

            • Trent Buck

              #7
              Re: macro style guideline

              Quoth Vyom on or about 2004-11-21:[color=blue]
              > 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.[/color]

              Then, why do you need a macro at all?

              -t

              Comment

              • Dan Pop

                #8
                Re: macro style guideline

                In <bd472b6d.04112 10600.77689620@ posting.google. com> vyom2k4@yahoo.c om (Vyom) writes:
                [color=blue]
                >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[i][j]=='x')[/color]

                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: Dan.Pop@ifh.de
                Currently looking for a job in the European Union

                Comment

                Working...