473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

macro explanation

I've come accross the following macro:

#define ARR_DIMS(a) \ 66 ((int *) (((char *) (a)) + sizeof(ArrayType)))

I know what it means (from reading the rest of the code)., but I wonder
could someone please explain how to interpret it ?

Jul 26 '06 #1
2 1665
On Wed, 26 Jul 2006, Bit Byte wrote:
I've come accross the following macro:

#define ARR_DIMS(a) \ 66 ((int *) (((char *) (a)) + sizeof(ArrayType)))

I know what it means (from reading the rest of the code)., but I wonder could
someone please explain how to interpret it ?
I believe that this macro will not even compile. First,
``\ 66'' does not make sense here. Second, the cast to int *
might invoke undefined behaviour. Third, if ARR_DIMS(a) means
what I think it means, then it should be doing this instead:

#define ARR_DIMS(a) (sizeof (a) / sizeof *(a))

Tak-Shing
Jul 26 '06 #2
Tak-Shing Chan <t.****@gold.ac.ukwrites:
On Wed, 26 Jul 2006, Bit Byte wrote:
>I've come accross the following macro:

#define ARR_DIMS(a) \ 66 ((int *) (((char *) (a)) + sizeof(ArrayType)))

I know what it means (from reading the rest of the code)., but I
wonder could someone please explain how to interpret it ?

I believe that this macro will not even compile.
Well, assuming it does work, my guess is that what you actually have is:

65 #define ARR_DIMS(a) \
66 ((int *) (((char *) (a)) + sizeof(ArrayType)))

The compiler deletes <'\' newline>, thus pasting the two
lines together.

'66' makes no sense, so maybe that's a line number displayed
by some tool and not part of the program.

((char *) (a)) + sizeof(ArrayType) points 'sizeof(ArrayType)'
bytes beyond the pointer 'a', which presumably points to
an object of type ArrayType. (A struct describing an array?)

(int*) casts that pointer to int*, thus you get a pointer to
a chunk of 'int's memory immediately beyond the ArrayType object.

Finally, the () surrounding the whole thing and the () around the 'a' is
to make sure it can be used as a normal expression, otherwise e.g.
ARR_DIMS(a + 3)
would be wrong - the '+ 3' would apply to (char *)a instead of to a.

--
Hallvard
Jul 26 '06 #3

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

Similar topics

3
2061
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not...
5
1888
by: Ken | last post by:
Hi. This is a followup to my NULL macro vs. 0 question. It's in a separate thread, because I need to post using google, so my original thread isn't available to me yet :(. Anyway, I just...
8
10848
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
44
3685
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
9
7646
by: Chen Shu | last post by:
Hi there: These days I came to a problem that I failed to convert a binary number to a hex number using a macro. For example: int a = BINARY4(0101) will become int a = 0x5; int a =...
2
12441
by: musa.biralo | last post by:
Hi, i want to read each row of specific column of musa.dbf using macro (VBA) in excel. my main gole is to copy the data of dbf file to excel file. I am new but want to do something..i tried and...
2
6060
by: Linny | last post by:
Hi, I have picked this piece of code(slightly modified) from one of gimpel software's lint page "bug of the month" #include <stdio.h> #define ON_THE_ROCKS const char *ice() { #if...
10
2258
by: gouqizi.lvcha | last post by:
Hi Friends, I saw a usage of macro like #define B3 "\xA\xB\xC" I don't understand why B3 is digital 10, can ayone point what the logic behind this usage. Rick
16
2757
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
36
2510
by: sh.vipin | last post by:
how to make large macro paste the code as it is Problem Explanation '-- For example in the program below /* a.c - starts here */ #define DECL_VARS() \ unsigned int a0;\ unsigned int a1;\...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.