macro style guideline

V

Vyom

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')
 
T

Trent Buck

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
 
M

Mark McIntyre

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...
 
R

Robert Gamble

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
 
A

Arthur J. O'Dwyer

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
 
V

Vyom

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.
 
T

Trent Buck

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
 
D

Dan Pop

In said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top