473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Nov 14 '05 #1
7 1778
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
Nov 14 '05 #2
On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , vy*****@yahoo.com (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 =----
Nov 14 '05 #3
On Sun, 21 Nov 2004 15:41:03 +0000, Mark McIntyre wrote:
On 21 Nov 2004 06:00:34 -0800, in comp.lang.c , vy*****@yahoo.com (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
Nov 14 '05 #4

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
Nov 14 '05 #5
> 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

Nov 14 '05 #6
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
Nov 14 '05 #7
In <bd**************************@posting.google.com > vy*****@yahoo.com (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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
22
by: beliavsky | last post by:
Is there a more recent set of Python style guidelines than PEP 8, "Style Guide for Python Code", by van Rossum and Warsaw, at http://www.python.org/peps/pep-0008.html , which is dated July 5, 2001?
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
31
by: bart | last post by:
Why is it that everybody likes the follwowing curly braces syntax function() { implementations; } I find the following much better to read (C style) function() {
16
by: E. Robert Tisdale | last post by:
C++ Programming Style Guidelines http://geosoft.no/development/cppstyle.html I think that these guidelines are almost *all* wrong. For example: 11. Private class variables should have...
40
by: rayw | last post by:
I've been trying to write a program that converts an entered number into its binary representation using three different techniques - bit shifting, mod 2, and itoa.. However, I'm getting 'snow...
18
by: pocmatos | last post by:
Hi all, While I was programming 5 minutes ago a recurring issue came up and this time I'd like to hear some opinions on style. Although they are usually personal I do think that in this case as...
28
by: Jack Morgan | last post by:
I got a macro like this in my code. > #define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum, name, menuname)\ static SOMEWNDSTRUCT &GetSomeWndStruct()\ {\ static SOMEWNDSTRUCT...
0
by: derelict | last post by:
Hey all, im getting desperate now. I have a macro running in Word 2003, when I run the macro it *should* put a 'bottom' cell border in each cell that has the style used - this included a border at...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.