• C

labels and goto statements....

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.
LVL 2
kellyjjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PhilCCommented:
You need skip_it:; to make a statement.
0
PhilCCommented:
That is: skip_it:; - with a colon, and a semi-colon.
0
kellyjjAuthor Commented:
Are talking about this:  

goto skip_it:;

???
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

PhilCCommented:
using continue; would be better programming style.
0
PhilCCommented:
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
alexoCommented:
>> 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
kellyjjAuthor Commented:
sorry dude,

You really didn't answer what was wrong.  alexo, thanks,  post an answer and you get the points.  
0
PhilCCommented:
I DID answer what was wrong, I just DIDN'T answer the question!
0
sgantaCommented:
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
alexoCommented:
>> alexo, thanks,  post an answer and you get the points.
No can do.  Question locked.
0
sgantaCommented:
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
kellyjjAuthor Commented:
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
sgantaCommented:
Hai Kellyjj
Thanks for your comment. I think alexo is 100% correct
- Thanks & Regards
0
alexoCommented:
Resubmitting comment from "Tuesday, May 26 1998 - 11:38AM PDT" as answer.

Have Fun!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kellyjjAuthor Commented:
Mr alexo,   thanks dude.
0
kellyjjAuthor Commented:
Ok,  why didn't this take my grade???
0
alexoCommented:
Think it did.  Have fun!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.