Advertisements

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

Advertisements

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.

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

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

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

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

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.

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

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!