CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

goto label question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Chubbs



Joined: 02 May 2005
Posts: 14

View user's profile Send private message

goto label question
PostPosted: Thu May 19, 2005 7:47 pm     Reply with quote

Can the
Code:
goto label;
label:

only be used in the same function?

If so is there any way you could skip from one bit of code to another?
I wanted to jump back to a point in a while loop from a function that is called from within the loop:


Code:
while
{
...
...
label:
...
...
function;
}

function()
{
...
...
goto label;
}

Obvoiusly this does not work, is there a way of doing this??
Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 19, 2005 8:26 pm     Reply with quote

Here are some examples of doing it by normal methods:

// This method uses a goto statement.
Code:

void main()
{
char c;

while
  {
   c = 0x55;

label:
   result = function();
   if(result == ERROR_CODE_5)
      goto label;
  }

//==================
function()
{

return(ERROR_CODE_5);
}



This method uses an inner while() loop and the continue statement.
Code:
void main()
{
char c;

while
  {
   c = 0x55;

   while(1)
     {
      result = function();
      if(result == ERROR_CODE_5)
         continue;   // Go to the start of the inner while() loop   
     }
     
  }

//----------------
function()
{

return(ERROR_CODE_5);
}
Chubbs



Joined: 02 May 2005
Posts: 14

View user's profile Send private message

PostPosted: Thu May 19, 2005 11:18 pm     Reply with quote

Thanks once again, I was able to successfully use it in my program.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group