More C programming questions ...

Vu Pham vu
Tue Feb 15 11:44:43 PST 2005


> -----Original Message-----
> From: linux-users-bounces at linux-sxs.org 
> [mailto:linux-users-bounces at linux-sxs.org] On Behalf Of Ben Duncan
> Sent: Tuesday, February 15, 2005 9:54 AM
> To: Linux tips and tricks
> Subject: More C programming questions ...
> 
> Can a "break" be used to bust out of a for loop:
> 
> example:
> 
> int idx = 0 ;
> int DONEGOOD = 0 ;
> int numberitems = 64 ;   /* Limit items we can have to 64 */
> char string[256] ;       /* MAKE big enuff so we do not overflow
>                              mydata in the file will always be 32  */
> 
> for (idx = 0 ; idx < numberitems ; idx++ ) {
>      gogetmydata( string ) ;
>      if ( strncmp( string, "ITEMID", 6) == 0 )
>         {
>           DONEGOOD = 1 ;
>           break ;
>          }
> }
> 
> 
> AND/OR, is there a better way of doing this ?
> 

Yes, break will break the loop.
I often do similar code for the compare/break.

Personally I avoid using global variables as much as possible. I guess you
use idx as global var because you do not include it in gogetmydata. 
Then in this case, string is able to be accessed directly without using it
as formal parameter in gogetmydata. IOW, there may be two versions of
gogetmydata :
 int gogetmydata(void) : use global vars of idx and string
Or
 int gogetmydata( int index, char * pstring ) : use local idx and string

Just my $0.02.

Vu



More information about the Linux-users mailing list