Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array-problems [newbie]

Reply
Thread Tools

Array-problems [newbie]

 
 
LibraryUser
Guest
Posts: n/a
 
      08-31-2003
The Real OS/2 Guy wrote:
> Tim Hagan <(E-Mail Removed)> wrote:
>

.... snip ...
> >
> > You could try something like the following instead. (There are
> > probably more efficient ways to do this.)
> >
> > int parse_args(char cmd_arg[])
> > {
> > if (strcmp(cmd_arg, "test1") == 0)
> > {
> > printf("%s\n", cmd_arg);
> > return 0;
> > }
> > else if (strcmp(cmd_arg, "test2") == 0)

>
> The else is superflous here as when the if is true the codeflow
> ends with the return statement. So only the case that if fails
> is over to handle.
>
> > {
> > printf("%s\n", cmd_arg);
> > return 0;
> > }
> > else

>
> Another superflous else
>
> > return 1;
> > }


On the contrary, the "superfluous" elses are extremely useful for
emphasizing the one-of-many conditions met. There is no
intrinsic reason for the individual cases to exit with a 'return'
statement, and the code would be clearer without such (single
exit point).

This is the type of structure used to implement a switch on
conditions that are not usable as switch cases.

In general, don't obfuscate code for evanescent possible
optimizations.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-02-2003


Tim Cambrant wrote:
>
> "Karl Heinz Buchegger" <(E-Mail Removed)> skrev i meddelandet
> news:(E-Mail Removed)...
> >
> > In real code, things are often not that simple
> >

>
> What do I need to do, more than replacing every series of eight spaces in a
> file (or, every '\t') with four spaces? Of course that would mess up files
> with strings that contain eight spaces, but that could be avoided further.
> Of course it would be easier to use regular expressions, but since C doesn't
> support this in the standard libraries (or does it?), you would be stuck
> with comparing strings.
>
> Please correct me if I'm wrong.


As I said: In real programs, things are not that simple.
But it is a good exercise: write such a program and let it
reindent some source code, look at the result and see
if the result is better or worse then what you started with.
Also try source code which contains intended

* line breaks, as in

Diagonal = sqrt( ( X * X ) +
( Y * Y ) );

* tables, as in

IMPLEMENT_SERIAL( CGeomPrim, CObject, VERSIONABLE_SCHEMA | 1 );
IMPLEMENT_SERIAL( CGeomDataPrim, CGeomPrim, VERSIONABLE_SCHEMA | 1 );
IMPLEMENT_SERIAL( CGeomPoint, CGeomPrim, VERSIONABLE_SCHEMA | 1 );
IMPLEMENT_SERIAL( CLine, CGeomPrim, VERSIONABLE_SCHEMA | 1 );
IMPLEMENT_SERIAL( CCircle, CGeomPrim, VERSIONABLE_SCHEMA | 1 );
IMPLEMENT_SERIAL( CArc, CGeomPrim, VERSIONABLE_SCHEMA | 1 );
IMPLEMENT_SERIAL( CBezier, CGeomPrim, VERSIONABLE_SCHEMA | 1 );

* etc ...

Be sure to feed real world programs through your indenter!

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Tim Cambrant
Guest
Posts: n/a
 
      09-02-2003
"Karl Heinz Buchegger" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed)...
> As I said: In real programs, things are not that simple.
> But it is a good exercise: write such a program and let it
> reindent some source code, look at the result and see
> if the result is better or worse then what you started with.
> Also try source code which contains intended
>
> * line breaks, as in
>
> Diagonal = sqrt( ( X * X ) +
> ( Y * Y ) );
>
> * tables, as in
>
> IMPLEMENT_SERIAL( CGeomPrim, CObject, VERSIONABLE_SCHEMA |

1 );
> IMPLEMENT_SERIAL( CGeomDataPrim, CGeomPrim, VERSIONABLE_SCHEMA |

1 );
> IMPLEMENT_SERIAL( CGeomPoint, CGeomPrim, VERSIONABLE_SCHEMA |

1 );
> IMPLEMENT_SERIAL( CLine, CGeomPrim, VERSIONABLE_SCHEMA |

1 );
> IMPLEMENT_SERIAL( CCircle, CGeomPrim, VERSIONABLE_SCHEMA |

1 );
> IMPLEMENT_SERIAL( CArc, CGeomPrim, VERSIONABLE_SCHEMA |

1 );
> IMPLEMENT_SERIAL( CBezier, CGeomPrim, VERSIONABLE_SCHEMA |

1 );
>
> * etc ...
>
> Be sure to feed real world programs through your indenter!


Thanks for the help. I've written an indent-program which recreates the
source-file from "\t"'s to " ". I suppose this will do it. However, some
tabs are just not translated, so I'm thinking that there must be another
character than "\t" created by Vim (my editor), especially since I can't
move my cursor "over" these tabs in Vim. I should check the file in hex, but
i havn't had the time yet.

Once I fix this, I'm off to try some conditions and such.

--
Tim Cambrant
<tim at cambrant dot com>


 
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




Advertisments