Solved

labels and goto statements....

Posted on 1998-05-26
17
270 Views
Last Modified: 2010-04-15
Hello,

I am working on a SCO unix box.  Is there any kind of limits of labels and goto statements??  Is there any kind of rules??  

Here is my source code:

void compare_()
{
int done=FALSE;
int cnt=0,buf_cnt=0;
int fc=0;
char nl[]="\n";
char extrct[80];
char extrct2[80];
char fpath[]="  ";
char *ptr;
int a,tmp1,tmp2;

    printf("\n build_str_ary2 \n");
    printf("        ");

    while (!done)
    {
        if ((cnt % 100)==0)
        {
            printf(" %i ",cnt);
        }
        cnt++;


        y=0;
        //clear out array for next line
        for (a=0;a<80;a++)  extrct[a]=NULL;

        // get line
        do
        {
            fread(buf,1,1,f2);
            y++;

            if (y==1)   strcpy(buffer1,buf);
            else           strcat(buffer1,buf);

            if (feof(f2))
            {
                done=TRUE;  goto lbl;
            }
        } while (buf[0]!=10);
lbl:
        // parse out name of dml
        fc=0;
        for (a=0;a<strlen(buffer1);a++)
       {
           // if not parse char then build string
           if (buffer1[a]!=47)
           {
               if (fc!=0)
               {
                   buf[0]=buffer1[a];
                   strcat(extrct,buf);
               }
               else
               {
                   fc++;
                   buf[0]=buffer1[a];
                   strcpy(extrct,buf);
               }
           }
           else
           {
               // if parse char then check to see if we have gottent the name
               // of the dml.
               fc=0;
               ptr=strstr(extrct,"dml");
               if (ptr!=NULL)

                {
                    goto endLoop;
                }
            }
        } // end of for loop
endLoop:
        y=0;
        // strip off file ext.
        while (y<strlen(extrct))
        {
            buf[0]=extrct[y];
            if (buf[0]=='.') break;

            if (y==0) strcpy(extrct2,buf);
            else   strcat(extrct2,buf);
            y++;
        }

        //compare and if == then write out
        strcpy(extrct,extrct2);
        tmp1=strcmp(extrct,"sort");
        tmp2=strcmp(extrct,"writeoff");
        if ( (tmp1==0) || (tmp2==0) ) goto skip_it;


        for (a=0;a<1023;a++)
        {

            if (strcmp(buffy[a],extrct)==0)
            {
                fwrite(extrct,1,strlen(extrct),f3);
                fwrite(fpath,1,strlen(fpath),f3);
                fwrite(buffer1,1,strlen(buffer1),f3);
                fwrite(nl,1,strlen(nl),f3);
                break;
            }
        }
skip_it:
    } // while !done
}

 error: Syntax error before or at: }
 error: label redefined: lbl
 error: label redefined: endLoop

If I take out the code dealing with the label 'skip_it'  then it will compile just fine.   What have I done wrong??  It seems I can't see what I am doing wrong.

Thanks.
0
Comment
Question by:kellyjj
  • 5
  • 5
  • 4
  • +1
17 Comments
 

Expert Comment

by:PhilC
ID: 1250978
You need skip_it:; to make a statement.
0
 

Expert Comment

by:PhilC
ID: 1250979
That is: skip_it:; - with a colon, and a semi-colon.
0
 
LVL 2

Author Comment

by:kellyjj
ID: 1250980
Are talking about this:  

goto skip_it:;

???
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Expert Comment

by:PhilC
ID: 1250981
using continue; would be better programming style.
0
 

Expert Comment

by:PhilC
ID: 1250982
No, since the label is before the ending bracket of the while loop, your label needs to appear as a statement ending with a semi-colon. Instead of goto skip_it; try:
if ( (tmp1==0) || (tmp2==0) ) continue;
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250983
>> Is there any kind of limits of labels and goto statements??  Is there any kind of rules??

1) The label must be followed by a statement (even if it is an empty statement).

2) The label must be in the same function as the goto statement.
    (You can use setjmp/longjmp for non-local goto.  Good for REALLY incomprehensible code).

3) It is illegal to jump past a declaration with an initializer unless:
    *  The declaration is enclosed in a block that is not entered.
    *  The jump is from a point where the variable has already been initialized.

0
 
LVL 2

Author Comment

by:kellyjj
ID: 1250984
sorry dude,

You really didn't answer what was wrong.  alexo, thanks,  post an answer and you get the points.  
0
 

Expert Comment

by:PhilC
ID: 1250985
I DID answer what was wrong, I just DIDN'T answer the question!
0
 
LVL 4

Expert Comment

by:sganta
ID: 1250986
Hello !
   I am having solution for your problem without usint labels. As per software standards
you better to avoid using GOTO statements. It is worst practice using GOTO statements.
Here I am slightly modifying your code, wherever I modify,I am passing comments against on each statements which are modified by me. I hope this will solve your
problem.  
                              Thanks and Regards - SGANTA


    void compare_()
    {
    int done=FALSE;
    int cnt=0,buf_cnt=0;
    int fc=0;
    char nl[]="\n";
    char extrct[80];
    char extrct2[80];
    char fpath[]="  ";
    char *ptr;
    int a,tmp1,tmp2;

        printf("\n build_str_ary2 \n");
        printf("        ");

        while (!done)
        {
            if ((cnt % 100)==0)
            {
                printf(" %i ",cnt);
            }
            cnt++;

            y=0;
            //clear out array for next line
            for (a=0;a<80;a++)  extrct[a]=NULL;

            // get line
            do
            {
                fread(buf,1,1,f2);
                y++;

                if (y==1)   strcpy(buffer1,buf);
                else           strcat(buffer1,buf);

                if (feof(f2))
                {
                    done=TRUE;
                    break;   /* It will come out from do while loop that is equivalent to
                                goto lbl; */
                }
            } while (buf[0]!=10);

            /* Here I had removed lbl : */
 
            // parse out name of dml
            fc=0;
            for (a=0;a<strlen(buffer1);a++)
            {
               // if not parse char then build string
               if (buffer1[a]!=47)
               {
                   if (fc!=0)
                   {
                       buf[0]=buffer1[a];
                       strcat(extrct,buf);
                   }
                   else
                   {
                       fc++;
                       buf[0]=buffer1[a];
                       strcpy(extrct,buf);
                   }
               }
               else
               {
                   // if parse char then check to see if we have gottent the name
                   // of the dml.
                   fc=0;
                   ptr=strstr(extrct,"dml");
                   if (ptr!=NULL)

                    {
                        break; /* Here it will come out from for loop that is equivalent to
                                  goto endLoop; */
                    }
                }
            } // end of for loop

            /* Here I had removed endLoop: */

            y=0;
            // strip off file ext.
            while (y<strlen(extrct))
            {
                buf[0]=extrct[y];
                if (buf[0]=='.') break;

                if (y==0) strcpy(extrct2,buf);
                else   strcat(extrct2,buf);
                y++;
            }

            //compare and if == then write out
            strcpy(extrct,extrct2);
            tmp1=strcmp(extrct,"sort");
            tmp2=strcmp(extrct,"writeoff");

            if ( !((tmp1==0) || (tmp2==0)) )  /* Here I have modified your if statement
                                                 which will execute for statement
                                                 only if not (tmp1 == 0 or tmp2 ==0)
                                                 which is equivalent to skip it if it is
                                                 false */
                /* I had removed goto skip_it */


            for (a=0;a<1023;a++)
            {

                if (strcmp(buffy[a],extrct)==0)
                {
                    fwrite(extrct,1,strlen(extrct),f3);
                    fwrite(fpath,1,strlen(fpath),f3);
                    fwrite(buffer1,1,strlen(buffer1),f3);
                    fwrite(nl,1,strlen(nl),f3);
                    break;
                }
            }
         /* Here I have removed skip_it which is not required */
        } // while (!done )
    }

I hope this is more than sufficient to you and acceptable to you.

Thanks & Regards - SGANTA
JESUS LOVES YOU - sganta
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250987
>> alexo, thanks,  post an answer and you get the points.
No can do.  Question locked.
0
 
LVL 4

Expert Comment

by:sganta
ID: 1250988
Hai kellyjj !
I am not worrying about points. You have to justify it.
I know how to use labels, but it is worst practice to use GOTO statement as per
the SOFTWARE standards.
So, that is why I took the pain to DEBUG and MODIFIED your code.
You have to justify it. About LABELS alexo is also correct.
Please justify the answer. GOD LOVES YOU - sganta
0
 
LVL 2

Author Comment

by:kellyjj
ID: 1250989
sganta --
 
Thanks for your effort.  However, I did not ask for alternate ways of doing it.  I am aware of commands like 'break' and continue.  What I was asking was for the rules/limits of using labels, and what was wrong with the label I was using. alexo rules pointed out to me exectly what I was doing wrong.

I will not argue that fact that using labels is not considered 'standard' coding practices.  And I do believe a over use of them can be rather confusing.   I do not think my code was an over use of them.   As a matter of fact if I was writing this code in assembler  I would have have used something like 'je skip_it'  as due to the fact that I don't want to jump far.  In other parts of my program I do use break.    
0
 
LVL 4

Expert Comment

by:sganta
ID: 1250990
Hai Kellyjj
Thanks for your comment. I think alexo is 100% correct
- Thanks & Regards
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1250991
Resubmitting comment from "Tuesday, May 26 1998 - 11:38AM PDT" as answer.

Have Fun!
0
 
LVL 2

Author Comment

by:kellyjj
ID: 1250992
Mr alexo,   thanks dude.
0
 
LVL 2

Author Comment

by:kellyjj
ID: 1250993
Ok,  why didn't this take my grade???
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250994
Think it did.  Have fun!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now