Array-problems [newbie]

L

LibraryUser

The said:
.... 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.
 
K

Karl Heinz Buchegger

Tim said:
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!
 
T

Tim Cambrant

Karl Heinz Buchegger said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top