tags:

views:

31

answers:

3

Hi,

How to write a macro which calls goto END_ label?

For ex:

#define MY_MACRO() \
//How to define goto END_##function_name label??

my_function()
{
    MY_MACRO();

END_my_function:
   return;
}

The MY_MACRO should simply replace with the line

goto END_my_function;
+1  A: 

I don't think it can be done. Even though some compilers define __FUNCTION__ or __func__, these are not expanded in macros.

However, note that you don't need a separate label for each function: you can use END for all functions and simply write goto END.

lhf
`__func__` is C99, and this is a string, not an identifier.
bstpierre
Yeah! I ended up doing that! :)
Bharathwaaj
A: 

Use token concatenation.

#define MY_MACRO( function_name ) END_ ## function_name
Praetorian
The point seems to be to avoid giving `function_name` explicitly.
lhf
Doh! I see that now, sorry.
Praetorian
A: 

The preprocessor does not know what function it is in. But you can get close -- you'll have to pass the function name to this macro

#include <stdio.h>

#define GOTO_END(f) goto f ## _end

void foo(void)
{
   printf("At the beginning.\n");

   GOTO_END(foo);

   printf("You shouldn't see this.\n");

  foo_end:
   printf("At the end.\n");
   return;
}

int main()
{
   foo();
}
bstpierre