Re: Function with two or more entry point

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Mar 12, 2013 at 1:17 PM, <sztfg@xxxxxxxxx> wrote:
>
> It is possible to make function that have two or more entry points?
> Something like this:
>
> #include <stdio.h>
>
> void test_print1()
> {
>   printf("print1\n");
>   void test_print2()
>   {
>     printf("print2\n");
>   }
> }
>
> int main ()
> {
>   test_print1();
>   test_print2();
>
>   return 0;
> }
>
> I also tried this:
>
> #include <stdio.h>
> #include <stddef.h>
>
> void test_print1()
> {
>   entry0:
>   printf("print1\n");
>   entry1:
>   printf("print2\n");
> }
>
> const ptrdiff_t ent1_m_ent0 =  (&&entry1 - &&entry0);
>
> int main ()
> {
>   test_print1();
>   (*(void(*)())(char *)(test_print1)+ ent1_m_ent0 )();
>   return 0;
> }
>
> ... but it doesn't work

This works, and is perhaps close to the idea:

#include <stdio.h>

typedef void (*funcptr)();

funcptr test_print(int pick)
{
  void test_print1()
  {
    printf("print1\n");
  }
  void test_print2()
  {
    printf("print2\n");
  }
  return (pick == 1) ? test_print1 : test_print2;
}

int main ()
{
  funcptr ep1 = test_print(1);
  funcptr ep2 = test_print(2);

  ep1();
  ep2();

  return 0;
}



--
James Bowman
http://www.excamera.com/


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux