Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Calling macros from outside the definition

Reply
Thread Tools

Calling macros from outside the definition

 
 
Ben
Guest
Posts: n/a
 
      06-16-2006
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
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      06-16-2006
On Fri, 16 Jun 2006 13:00:04 +1000, Ben <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ben
Guest
Posts: n/a
 
      06-16-2006
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
 
Reply With Quote
 
$hiv.....
Guest
Posts: n/a
 
      06-16-2006

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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-16-2006
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


 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      06-19-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
macros-loop? calling macros X times? Andrew Arro C Programming 2 07-24-2004 09:52 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments