Calling macros from outside the definition

B

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
 
B

Barry Schwarz

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
 
B

Ben

Barry said:
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
 
$

$hiv.....

Ben said:
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
 
C

CBFalconer

Ben said:
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.
 
B

Ben

CBFalconer said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top