Global Labels in C++

G

gullu

Hi,

I know that it is a very wierd question but anyways, I will ask it and
will really appreciate of someone could help me out.

Is there any way to use global labels in C/C++.

I want to implement a piece of code that has a functionality similar
to the code shown below.

int main()
{
LabelTest();
printf("This MUST NOT be printed.\n");
label1:
print("This MUST be printed.\n");

return 0;
}

void LabelTest()
{
goto label1;
}

Is there anyway to achieve some functionality like this.

Thanks,
Atif
 
J

Jim Langston

gullu said:
Hi,

I know that it is a very wierd question but anyways, I will ask it and
will really appreciate of someone could help me out.

Is there any way to use global labels in C/C++.

I want to implement a piece of code that has a functionality similar
to the code shown below.

int main()
{
LabelTest();
printf("This MUST NOT be printed.\n");
label1:
print("This MUST be printed.\n");

return 0;
}

void LabelTest()
{
goto label1;
}

Is there anyway to achieve some functionality like this.

Yes, by redesigning the program. A program that needs a goto is usually
poorly designed. For instance...

int main()
{
if ( PrintLabel() )
printf("This MUST NOT be printed \n");
printf("This MUST be printed \n");

return 0;
}

bool PrintLabel()
{
return false;
}

If you are trying to find ways to use GOTOs better in a C++ program, then
what you probably actually need to do is learn OOPs programming. What is it
you hope to achieve by using a goto anyway?

Regards,

Jim Langston
 
A

aiooua

I know that it is a very wierd question but anyways, I will ask it and
will really appreciate of someone could help me out.

Is there any way to use global labels in C/C++.

firstly, i am certain there is absolutely no need for writing such
code. you can achieve whatever it is you're trying to do, without such
gymnastics.

but here's something that can do what you're looking for.

----
#include <setjmp.h>
#include <stdio.h>
jmp_buf env;
void LabelTest();
int main()
{
if(setjmp(env))
{
LabelTest();
printf("This MUST NOT be printed.\n");
}
printf("This MUST be printed.\n");
return 0;

}

void LabelTest()
{
longjmp(env,1);
}
 
N

none

aiooua a écrit :
firstly, i am certain there is absolutely no need for writing such
code. you can achieve whatever it is you're trying to do, without such
gymnastics.

but here's something that can do what you're looking for.

----
#include <setjmp.h>
#include <stdio.h>
jmp_buf env;
void LabelTest();
int main()
{
if(setjmp(env))
{
LabelTest();
printf("This MUST NOT be printed.\n");
}
printf("This MUST be printed.\n");
return 0;

}

void LabelTest()
{
longjmp(env,1);
}
----

try not to use it, in real-life code.

thanks,

Juste a little correction...

setjmp returns 0 if returning directly and non-zero when returning from
longjmp() using the saved context so to work the above code needs to
have its "if" statement inverted, like this

if(setjmp(env)==0)
{
LabelTest();
printf("This MUST NOT be printed.\n");
}
 
A

Andre Kostur

Hi,

I know that it is a very wierd question but anyways, I will ask it and
will really appreciate of someone could help me out.

Is there any way to use global labels in C/C++.

I want to implement a piece of code that has a functionality similar
to the code shown below.

int main()
{
LabelTest();
printf("This MUST NOT be printed.\n");
label1:
print("This MUST be printed.\n");

return 0;
}

void LabelTest()
{
goto label1;
}

Is there anyway to achieve some functionality like this.

Yeah... rethink what you're doing:


class GotoLabel1
{
};

void LabelTest()
{
throw GotoLabel1();
}

int main()
{
try
{
LabelTest();
printf("This MUST NOT be printed.\n");
}
catch (const GotoLabel1 &)
{
}

printf("This MUST be printed\n");
return 0;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,282
Latest member
Xyprime

Latest Threads

Top