470,855 Members | 1,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Calling macros from outside the definition

Ben
This is a follow-on from the "Help with array/pointer segmentation fault needed" thread..

I have not been able to find much information on the rules for macros. I want to be able to call a macro from outside it's
definition which I am fairly sure I have seen done before. This code works:

#define TEST(where,dimension,ptr) \
printf ("\tAll %s in %s values: ",where,dimension); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointer->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");
TEST("prev","block ",prev_in_block);
TEST("prev","column",prev_in_column);
TEST("prev","row ",prev_in_row);
#undef TEST
But this does not:

#define TEST(where,dimension,ptr) \
printf ("\tAll %s in %s values: ",where,dimension); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointer->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");

#undef TEST
TEST("prev","block ",prev_in_block);
TEST("prev","column",prev_in_column);
TEST("prev","row ",prev_in_row);

with errors:

[Warning] implicit declaration of function `TEST'
`prev_in_block' undeclared (first use in this function)
`prev_in_column' undeclared (first use in this function)
`prev_in_row' undeclared (first use in this function)
[Warning] unused variable `pointer'

The reason for this is I have several blocks of code where only the inner loop needs to be macroed, the preceeding lines of each
block are not suitable for macroing.

Can anyone point me to a good reference or just explain what I need to do call a macro from outside it's definition (if it's
possible)?
cheers,

Ben
Jun 16 '06 #1
5 1596
On Fri, 16 Jun 2006 13:00:04 +1000, Ben <be*********@spam.me> wrote:
This is a follow-on from the "Help with array/pointer segmentation fault needed" thread..

I have not been able to find much information on the rules for macros. I want to be able to call a macro from outside it's
definition which I am fairly sure I have seen done before. This code works:

#define TEST(where,dimension,ptr) \
printf ("\tAll %s in %s values: ",where,dimension); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointer->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");
TEST("prev","block ",prev_in_block);
TEST("prev","column",prev_in_column);
TEST("prev","row ",prev_in_row);
#undef TEST
But this does not:

#define TEST(where,dimension,ptr) \
printf ("\tAll %s in %s values: ",where,dimension); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointer->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");

#undef TEST
TEST("prev","block ",prev_in_block);
How could it possibly work? The #undef removes the macro. At this
point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and trying to
reference it outside the block. It no longer exists once you leave
the block. TEST("prev","column",prev_in_column);
TEST("prev","row ",prev_in_row);

with errors:

[Warning] implicit declaration of function `TEST'
`prev_in_block' undeclared (first use in this function)
`prev_in_column' undeclared (first use in this function)
`prev_in_row' undeclared (first use in this function)
[Warning] unused variable `pointer'

The reason for this is I have several blocks of code where only the inner loop needs to be macroed, the preceeding lines of each
block are not suitable for macroing.

Can anyone point me to a good reference or just explain what I need to do call a macro from outside it's definition (if it's
possible)?
cheers,

Ben

Remove del for email
Jun 16 '06 #2
Ben
Barry Schwarz wrote:

How could it possibly work? The #undef removes the macro. At this
point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and trying to
reference it outside the block. It no longer exists once you leave
the block.

Ok so it blanket can't be done and I need to write a separate function (I get this is exactly what functions are for).

Funny though...I have a computer science textbook that repeatedly refers to a macro definition for creating structures, where
the labels of members are given to the macro. Now since they are only doing this once per 'program', what's the point of having
a macro? It's just a regular definition of a structure. I guess they were just trying to save space in the textbook, but I took
it to mean they were calling it from outside the definition.

thanks
Jun 16 '06 #3

Ben wrote:
Barry Schwarz wrote:

How could it possibly work? The #undef removes the macro. At this
point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and trying to
reference it outside the block. It no longer exists once you leave
the block.

Ok so it blanket can't be done and I need to write a separate function (I get this is exactly what functions are for).

Funny though...I have a computer science textbook that repeatedly refers to a macro definition for creating structures, where
the labels of members are given to the macro. Now since they are only doing this once per 'program', what's the point of having
a macro? It's just a regular definition of a structure. I guess they were just trying to save space in the textbook, but I took
it to mean they were calling it from outside the definition.

thanks


just remove the #undef for the macro TEST

Jun 16 '06 #4
Ben wrote:
Barry Schwarz wrote:
How could it possibly work? The #undef removes the macro. At
this point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and
trying to reference it outside the block. It no longer exists
once you leave the block.


Ok so it blanket can't be done and I need to write a separate
function (I get this is exactly what functions are for).


I think you are confused about what terminates the macro
definition. That is done by the end of the line doing the
definition, which is why you have the line continuation
operations. #undef discards the whole definition - gone, killed,
never to chirp again.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 16 '06 #5
Ben
CBFalconer wrote:
Ben wrote:
Barry Schwarz wrote:
How could it possibly work? The #undef removes the macro. At
this point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and
trying to reference it outside the block. It no longer exists
once you leave the block.

Ok so it blanket can't be done and I need to write a separate
function (I get this is exactly what functions are for).


I think you are confused about what terminates the macro
definition. That is done by the end of the line doing the
definition, which is why you have the line continuation
operations. #undef discards the whole definition - gone, killed,
never to chirp again.

Not confused, didn't know! If you look at the other thread someone else provided the code for the macro. But thanks for
providing the answer.
cheers,
Ben
Jun 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Chris Reedy | last post: by
1 post views Thread by Rafal 'Raf256' Maj | last post: by
11 posts views Thread by Ben Hetland | last post: by
37 posts views Thread by hasadh | last post: by
8 posts views Thread by David | last post: by
20 posts views Thread by copx | last post: by
5 posts views Thread by Joakim Hove | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.