473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Global Labels in C++

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

Apr 11 '07 #1
4 9775
"gullu" <at********@gmail.comwrote in message
news:11*********************@y5g2000hsa.googlegrou ps.com...
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
Apr 11 '07 #2
On Apr 11, 10:28 am, "gullu" <atifhas...@gmail.comwrote:
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);
}
----

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

thanks,

Apr 11 '07 #3
aiooua a écrit :
On Apr 11, 10:28 am, "gullu" <atifhas...@gmail.comwrote:
>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);
}
----

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");
}

--
JF
Apr 11 '07 #4
"gullu" <at********@gmail.comwrote in news:1176269286.281154.52340
@y5g2000hsa.googlegroups.com:
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;
}

Apr 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
3
by: Dalan | last post by:
I need some assistance or advise in composing code for a global function module or a related one for populating values in text boxes on reports and forms with a name, actually several different...
6
by: DebbieG | last post by:
I am creating a database for a small department in a university. Each student in their database can have 2 mailing addresses. They wanted mailing labels pulling just the 1st address. No problem....
3
by: Grim Reaper | last post by:
I print mailing labels out of Access 2000 databases about 3 to 4 times a week. I have been having problems with one thing since I have been printing mailing labels. I print mailing labels by...
9
by: Shapper | last post by:
Hello, I am declaring a variable in my aspx.vb code as follows: Public Class catalogue Public productid As String Private Sub Page_Load ... I have an image button where I call the...
6
by: Ron | last post by:
Hi, I know Access allows for easy construction of a report setup to print labels from a table/query, etc. I've done that one. It works pretty well for what I need. However, is there an...
1
by: Fred | last post by:
Hi, I am working on converting a classic asp site to asp.net. The site has an include file full of constants that are used across the site. This file can be dynamically updated and any changes...
1
by: sap0321 | last post by:
This should be kindergarten stuff but for some reason I am having trouble with it. I do 99.9% web programming and this is the first windows app i've done in years - probably the first ever in C# ......
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.