[techtalk] switch function in C (or how to read commandline args?)

Penguina penguina at cosyn.co.nz
Thu Jul 5 16:06:57 EST 2001



"Tokenizing" is the right term.

My response was to CD's token defs

>CD>So, I can do something like:
>CD>
>CD>#define FORCE_SWITCH "--force"
>CD>#define CONFIG_SWITCH "-C"
>CD>
>CD>and so on?

> penguina at cosyn.co.nz said:
>P> No.

CD asked "Can I do X" this is P answering "No" to CD.
But P (me) wasn't entirely correct, musta been...tokin'

>P> What this will accomplish is setting the constants
>P> to be strings, which will then require slow strcmp
>P> processing as above.  When you tokenize something,
>P> you come up with a unique *numerical* constant to
>P> represent a particular string.  You can also have
>P> several different strings parse to the same token.
>P> So I could say
>P> #define HOLA   23
>P> #define HELLO  23
>P> #define AMIGO  24
>P> #define FRIEND 24

On Wed, 4 Jul 2001, Jeff Dike wrote:
J> Exactly, that's what I meant.  I should have been more clear about that.
J> Also, "tokenizing" might be the wrong term, but I don't know of a better one
J> offhand.

But actually a token doesn't necessarily need to be a number.
It's just a lot clearer what's going on when numbers are used,
and they're faster and easier to perform operations on.

Technically, there's nothing wrong with Conor's defining
literal string tokens as above (if she doesn't mind processing
them as such) but they wouldn't work in the following context:

>> >CD>
        case FORCE_SWTICH:
                force=1;
                break;
        case CONFIG_SWITCH:
                strcpy(config_file_name, argv[2]);
                break;
>CD> <<

in the above, had FORCE_SWITCH and CONFIG_SWITCH been tokenized
as ints, it would be OK, but in this example, they're strings.

Strings.  A Parable.

Three strings were walking down the road one day, and were very
thirsty.  After being rejected from two watering-holes, the third
string gets an idea.  He unravels all his strands and gets them in
an enormous tangle.  They enter the third watering hole.  The
publican looks down at them and says "You know, we don't serve
strings here.  You wouldn't happen to be a string, would you?"

And the third string answers, "No.  I'm afraid not."






More information about the Techtalk mailing list