This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Function with two or more entry point
- From: James Bowman <jamesb at excamera dot com>
- To: sztfg at yandex dot ru
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 12 Mar 2013 14:16:34 -0700
- Subject: Re: Function with two or more entry point
- References: <250151363119434@web23h.yandex.ru>
On Tue, Mar 12, 2013 at 1:17 PM, <sztfg@yandex.ru> 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/