'goto" gone from computer languages or is it!

Kevin Handy kth at srv.net
Tue May 17 10:48:58 CDT 2005


Dave Dunfield wrote:

>void print_arg(int *aryp, unsigned n)
>{
>   int i;
>   for(i=0; ;) {
>      printf("%u", aryp[i]);
>      if(++i >= n)
>         break;
>      fputs(", ", stdout); }
>   putc('\n', stdout);
>}
>
>
>  
>
What's wrong with this (assumes that aryp[0] exists,
which the above program also assumes), shorter,
more obvious as to what is happening, etc.:

void print_arg(int *aryp, unsigned n)
{
   printf("%u", aryp[0]);
   for(int i=1; i<n; i++)
      printf(", %u", aryp[i]);
   putc('\n', stdout);
}

or are we artifically limited to using a single printf?

You can fix the aryp[0] not existing by wrapping everything
except the 'putc' in a single if (executed once per call,
not once per element)

void print_arg(int *aryp, unsigned n)
{
   if (n >= 0) 
   {
      printf("%u", aryp[0]);
      for(int i=1; i<n; i++)
         printf(", %u", aryp[i]);
   }
   putc('\n', stdout);
}




More information about the cctalk mailing list