Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > acceptable use of goto?

Reply
Thread Tools

acceptable use of goto?

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-27-2008
João Jerónimo <(E-Mail Removed)> writes:

> Ben Bacarisse wrote:
>> void print_int_array(int *array, size_t arraylen)
>> {
>> putchar('(');
>> for (size_t i = 0; i < arraylen; i++)
>> printf("%s%d", (i ? ", " : ""), array[i]);
>> putchar(')');
>> }

>
> Are you aware that you are comparing the i variable twice in each
> iteration?


Yes. Are you aware the loop calls printf? If we were comparing "bit
hacks" that might be a concern, but in an output function?

> I think my solution with break ends being much more readable
> (and less overkill)...


Each to their own, as I said before. We could go at it, debating the
merits of various styles and coding practices, but Usenet is not a
good place to persuade people, so I have largely given up trying.
After about three posts each, it becomes impossible for either party to
say "oh, yes, yours is much clearer" and it usually takes three posts
to explain what one likes about ones own way of doing things --
stalemate is inevitable.

I've picked up lots of tips reading Usenet, but I don't recall anyone
agreeing they have been persuaded by a 367-message thread. It is best
just to show one's code, comment on preferences and move on.

--
Ben.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-27-2008
Ben Bacarisse said:

<snip>

> Each to their own, as I said before. We could go at it, debating the
> merits of various styles and coding practices, but Usenet is not a
> good place to persuade people, so I have largely given up trying.
> After about three posts each, it becomes impossible for either party to
> say "oh, yes, yours is much clearer"


Not *quite* impossible. I once persuaded an entrenched
strncpy-is-safer-than-strcpy supporter to change his view, after a
protracted debate that was notable for its civility.

So - it's not impossible, because at least one counter-example exists. But
it's pretty rare, nonetheless.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      03-27-2008
On 25 Mar, 16:14, Joo Jernimo <(E-Mail Removed)> wrote:

[exiting mid loop]

<snip>

> If I declare an integer array with:
> int myarray[] = {5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0};
> And want to write a routine to print it like this:
> (5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0)
> , my best hint would be (untested):
>
> --------------------------------------------------
> void print_int_array( int *array, int arraylen )
> {
> * *assert( arraylen >= 1 );
>
> * *int *arrayend = array+arraylen;
>
> * *printf("(");
> * *for(; {
> * * * printf("%d", *array);
> * * * array++;
> * * * if ( array == arrayend )
> * * * * *break;
> * * * printf(", ");
> * *}
> * *printf(")");}
>
> --------------------------------------------------
>
> Now, please point me out a better solution.
> (This is not strictly the case of a loop with multiple exits, but helps to
> ilustrate the idea...)



void printlist (int *a, int n)
{
printf ("%d", *a);

if (--n > 0)
{
printf (", ");
printlist (a, n);
}
}

void print_int_array_1 (int *array, int arraylen)
{
printf ("(");
printlist (array, arraylen);
printf (")");
}


--
Nick Keighley

"I wish to God these calculations had been accomplished by steam."
--C. Babbage
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      03-27-2008
Nick Keighley wrote:
> On 25 Mar, 16:14, Joo Jernimo <(E-Mail Removed)> wrote:
>
> [exiting mid loop]
>
> <snip>
>
>> If I declare an integer array with:
>> int myarray[] = {5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0};
>> And want to write a routine to print it like this:
>> (5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0)
>> , my best hint would be (untested):

....

>> Now, please point me out a better solution.
>> (This is not strictly the case of a loop with multiple exits, but
>> helps to ilustrate the idea...)

>
>
> void printlist (int *a, int n)
> {
> printf ("%d", *a);
>
> if (--n > 0)
> {
> printf (", ");
> printlist (a, n);
> }
> }
>
> void print_int_array_1 (int *array, int arraylen)
> {
> printf ("(");
> printlist (array, arraylen);
> printf (")");
> }


I think you want ++a in that printlist somewhere.

Also arraylen or n=0 is not a possibility.

--
Bart




 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      03-27-2008
Joo Jernimo wrote:
> Richard wrote:
>> You wouldn't. You especially would not want to print possibly
>> thousands of corrupt data elements as your code would. Always doe
>> the zero check in the called function. It should not be the job of
>> the calling code especially when there is zero overhead by doing it
>> properly as in Ben's example.

>
> assert() was invented long ago...
>
> However, I understand that in some conditions migth be helpfull to
> print empty arrays. That's a special case, anyway...


What's special about empty arrays?

In other, dynamic languages, arrays can have zero or more elements.

In C, possibly it's illegal to have a fixed array of 0 elements, but it's
certainly possibly to have a 100-element array of which the first N elements
have been filled, where N can be zero, and you may want to display those
active elements.

So an empty array with N=0, bracketed according to the example in this
thread, should be shown as ().

And with N=1 as (a)

And with N=2 as (a,b) and so on.

--
Bart


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Acceptable Load on WAN Link ciscoham Cisco 3 10-20-2005 04:28 PM
MCAD/MCSD certificate testing: whats an acceptable exam score rate .Net Sports ASP .Net 2 05-03-2005 05:31 PM
acceptable way to program steve Java 34 02-20-2005 06:37 PM
Is this trick with reset acceptable? valentin tihomirov VHDL 6 04-14-2004 05:52 PM
An "acceptable" use of break Chris Potter C Programming 26 11-07-2003 09:21 PM



Advertisments