By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,299 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,299 IT Pros & Developers. It's quick & easy.

Calling macros from outside the definition

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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.