how can i set jump from a function to the main function??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmaltech28
    New Member
    • Nov 2009
    • 2

    how can i set jump from a function to the main function??

    Code:
    void main()
    {
    ________            //statements....
    ________
    step:
    ________
    ________
    ________           //statements......
    }
    int fun()
    {
    goto step;         //Have to jump to the label step in main function.
    }                      //IS IT POSSIBLE, IF NOT TELL ME HOW CAN I DO IT.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Under no circumstances should you ever use the goto statement in c (or C++).

    Why don't you just return a value from fun that indicates what main should do next and use a if statement in main to make the execution path decision.

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Gotos have function scope which means you can not jump between functions. Be thankful that you can't because you could cause all kinds of problems if you did. Memory leaks and a corrupted stack are just the beginnings of the troubles you would encounter.

      As Banfa said, there is no reason to use gotos anymore. There are lots of alternatives that don't cause problems. The trick is to use the proper construct for the proper problem.


      If you want to ignore all of this good information, there are setjmp and longjmp system routines in unix. They only jump backwards, but are a great way to programmaticall y shoot yourself in the foot, hand, or head.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Structured programming, a revolutionary innovation in programming, was popularized in Edsger Dijkstra's seminal 1967 article "Goto statement considered harmful". Younger programmers aren't aware of the truly sorry state of programming when the only method available to alter program flow was the unconstrained goto statement. All they see is the reflexive antipathy to 'goto' from those of us old enough to remember the spaghetti code of yesteryear.

        Unlike the earlier respondents, I feel there is an valid use for the goto statement. I have found in rare instances that using 'goto' is helpful to trap error cases into a common clean-up snippet immediately before returning an error. In most cases, you can avoid the goto by either rearranging your logic or by refactoring a large function into several smaller functions.

        As a C programmer, I'm intrigued by the C++ exception handling mechanism. I'm not familiar enough with it to determine if it is the magic bullet that eliminates this last valid use of 'goto' or if it is itself a kind of 'goto' that should be avoided.

        Comment

        • Tassos Souris
          New Member
          • Aug 2008
          • 152

          #5
          RRick setjmp and longjmp are in the Standard C Library in <setjmp.h>.

          The only place i have seen calls to setjmp and longjmp, is from signal handlers where some clean-up work must be done. setjmp and longjmp effectively clear the stack but they are not "signal-safe" and they do not clear memory allocated on the heap... in other words, setjmp and longjmp and all other forms of "i am here and now i will go there" except by means of a function call is to be forbidden!!

          Really, we should post a list here of things that a programmer is forbidden to do, things that he/she is allowed to do only once, only twice and such stuff... :)

          Comment

          • RRick
            Recognized Expert Contributor
            • Feb 2007
            • 463

            #6
            There are other accepted forms of gotos in C.
            • A break statement in a loop or switch
            • Continue or next in a loop
            • A return in the middle of a subroutine
            • Even the normal If-then-else statements and loops are forms of gotos

            These types of gotos are acceptable because they are controlled and do not cause adverse or unpleasant side affects in the code. Once you start using gotos, you loose all of that.

            When Dijkstra came out with Structured Programming, people didn't believe you could program without gotos and just use the 3 constructs he defined (i.e. if-then-else, while loop, and simple statement). People actually dared Dijkstra to solve some problems with out gotos. He solve them, and solved them elegantly, and the rest is history.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by RRick
              • A break statement in a loop or switch
              • Continue or next in a loop
              • A return in the middle of a subroutine
              These 3 are certainly legal forms of altering execution path, however I have seen plenty of coding standards that discouraged or even outlawed them mainly in order to increase the maintainability of the code.

              Comment

              Working...