macro explanation

B

Bit Byte

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

Tak-Shing Chan

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
 
H

Hallvard B Furuseth

Tak-Shing Chan said:
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.
 

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

Similar Threads

macro 41
C macro 3
Understanding ::after pseudo selector 3
Python3 string slicing 2
Simple Program 0
Macro Expansion 4
I know VBA (macros) - nothing else! 0
Help with macro 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top