Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macro style guideline

Reply
Thread Tools

macro style guideline

 
 
Vyom
Guest
Posts: n/a
 
      11-21-2004
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
 
Reply With Quote
 
 
 
 
Trent Buck
Guest
Posts: n/a
 
      11-21-2004
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
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      11-21-2004
On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , http://www.velocityreviews.com/forums/(E-Mail Removed) (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 =----
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      11-21-2004
On Sun, 21 Nov 2004 15:41:03 +0000, Mark McIntyre wrote:

> On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , (E-Mail Removed) (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
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      11-22-2004

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
 
Reply With Quote
 
Vyom
Guest
Posts: n/a
 
      11-22-2004
> 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

 
Reply With Quote
 
Trent Buck
Guest
Posts: n/a
 
      11-22-2004
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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-23-2004
In <(E-Mail Removed) > (E-Mail Removed) (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[i][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: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is PEP-8 a Code or More of a Guideline? Paul McGuire Python 52 06-01-2007 12:05 PM
style guideline for naming variables? John Salerno Python 2 03-18-2006 01:18 AM
What's the guideline for dealing with unwanted chars in input stream? lovecreatesbeauty C Programming 17 01-01-2006 07:42 PM
Redundant behavior in coding guideline lovecreatesbeauty C Programming 2 10-27-2005 01:26 PM
Redundant behavior in coding guideline lovecreatesbeauty C Programming 0 10-27-2005 10:01 AM



Advertisments