Calling macros from outside the definition

Discussion in 'C Programming' started by Ben, Jun 16, 2006.

  1. Ben

    Ben Guest

    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
     
    Ben, Jun 16, 2006
    #1
    1. Advertising

  2. On Fri, 16 Jun 2006 13:00:04 +1000, Ben <> 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
     
    Barry Schwarz, Jun 16, 2006
    #2
    1. Advertising

  3. Ben

    Ben Guest

    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
     
    Ben, Jun 16, 2006
    #3
  4. Ben

    $hiv..... Guest

    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
     
    $hiv....., Jun 16, 2006
    #4
  5. Ben

    CBFalconer Guest

    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
     
    CBFalconer, Jun 16, 2006
    #5
  6. Ben

    Ben Guest

    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
     
    Ben, Jun 19, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    80
    Views:
    2,431
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    447
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    497
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    517
    Michael T. Babcock
    Nov 3, 2003
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    495
    S.Tobias
    Jul 24, 2004
Loading...

Share This Page