checking the contents of an array..

A

Advocated

This is a part of my code:
<snipped>
#define MAX_LINE_LENGTH 256
typedef struct {
char *name;
void (*function)(void);
} COMMAND;

void progCmd(void);
void prog2Cmd(void);

COMMAND commands[] = {
{"prog1", prog1Cmd},
{"prog2", prog2Cmd}
};

<snipped>

im reading in characters and storing them in an array like:

while((ch = getchar()) != '\n'){
InputString[j] = ch;
line[k] = ch;
j++;
k++;
}
InputString[j] = '\0';
line[k] = '\0';

This is working fine. An example of running my program would be:
$ hello world
this would print
token0 is hello
token1 is world

Is there a way, so i can check if token0 is prog1? if you get what i mean?
the aim is so, if token0 is not prog1 or prog2, or not something else in the
commands array, then printf("exit") or something.

Cheers for any ideas
 
M

Mike Wahler

Advocated said:
This is a part of my code:
<snipped>
#define MAX_LINE_LENGTH 256
typedef struct {
char *name;
void (*function)(void);
} COMMAND;

void progCmd(void);
void prog2Cmd(void);

COMMAND commands[] = {
{"prog1", prog1Cmd},
{"prog2", prog2Cmd}
};

<snipped>

im reading in characters and storing them in an array like:

while((ch = getchar()) != '\n'){
InputString[j] = ch;
line[k] = ch;
j++;
k++;
}
InputString[j] = '\0';
line[k] = '\0';

This is working fine. An example of running my program would be:
$ hello world
this would print
token0 is hello
token1 is world

Is there a way, so i can check if token0 is prog1? if you get what i mean?

if(strcmp(token0, "prog1") == 0)
/* token0 contains string "prog1" */
the aim is so, if token0 is not prog1 or prog2, or not something else in the
commands array, then printf("exit") or something.

iterate through your arrary, use 'strcmp()' to test each item.

-Mike
 
A

Advocated

mean?

if(strcmp(token0, "prog1") == 0)
/* token0 contains string "prog1" */


iterate through your arrary, use 'strcmp()' to test each item.

-Mike

Isnt there a quick way though to just check if the token 0 is not in
commands[] because what if commands[] contained 10000 of entries, would take
forever?
 
K

Kevin Goodsell

Advocated said:
This is a part of my code:
<snipped>
#define MAX_LINE_LENGTH 256
typedef struct {
char *name;

If you don't make this

const char *name;

then you are asking for trouble.
void (*function)(void);
} COMMAND;

void progCmd(void);
void prog2Cmd(void);

COMMAND commands[] = {
{"prog1", prog1Cmd},

"prog1" is a string literal. String literals cannot be modified, but a
mis-feature of the language allows you to make a (non-const) char* point
to a string literal, and no diagnostic is required. This is almost
always a bad idea, since it allows your program to attempt to modify a
string literal with no complaint from the compiler - instead you get
undefined behavior (often a program crash) at run-time.

If you make sure you only use const char * for string literals, then the
compiler will complain when you attempt to modify the string literal.
{"prog2", prog2Cmd}
};

<snipped>

im reading in characters and storing them in an array like:

I'm not familiar with the word 'im' and I can't find it in my dictionary.
while((ch = getchar()) != '\n'){

What about end-of-file? If you encounter EOF before a newline, this will
never terminate.
InputString[j] = ch;
line[k] = ch;
j++;
k++;
}
InputString[j] = '\0';
line[k] = '\0';

This is working fine. An example of running my program would be:
$ hello world
this would print
token0 is hello
token1 is world

Is there a way, so i can check if token0 is prog1? if you get what i mean?
the aim is so, if token0 is not prog1 or prog2, or not something else in the
commands array, then printf("exit") or something.

strcmp() (along with a loop through your 'commands' array) should do
what you want. The problem you are having isn't completely clear, so if
that doesn't answer your question you may have to clarify what the
question is.

-Kevin
 
A

Alexander Bartolich

followup to Advocated:
Isnt there a quick way though to just check if the token 0 is
not in commands[] because what if commands[] contained 10000
of entries, would take forever?

Yes, there is.
No, by definition only an infinite loop takes forever.

Go get a book on algorithms.
At your disposal is at least binary sort, hash tables and trees.
Depending on your environment you may already have implementations
of these concepts available.
 
K

Kevin Goodsell

Alexander said:
followup to Advocated:
Isnt there a quick way though to just check if the token 0 is
not in commands[] because what if commands[] contained 10000
of entries, would take forever?


Yes, there is.
No, by definition only an infinite loop takes forever.

Go get a book on algorithms.
At your disposal is at least binary sort,

Binary search, not sort.
hash tables and trees.
Depending on your environment you may already have implementations
of these concepts available.

The bottom line is that he'll need some kind of organization of the data
in order to do anything more efficient than a linear search. On the
other hand, there's probably no reason to bother with anything more
complex. If there's fewer than about 1000 elements linear search should
be plenty fast. I wouldn't even consider bothering with more complex
search methods unless the number of elements was above 1000, or unless
there was some compelling reason to need a very, very fast search that
made it worth the extra effort.

-Kevin
 
A

Advocated

Just to confirm what i want to do:
If this is my command array:

COMMAND commands[] = {
{"prog1", prog1Cmd},
{"prog2", prog2Cmd}
};

User enters: "ima test "
This is broken down and at the moment printed like:
Token0 = ima
Token1= test

It should then search the commands array by using token0. As "ima" is not in
the commands array, ill set it to do something(Quit,or something)... if it
is in the array, well thats something ill sort afterwards.

if token0 is in commands[] then quit, else do something else

After thinking, i do think strcmp() is the right way to go about it, once i
work out exactly what i need.
Hope this clarifies the problem, and cheers for advice so far. I dont think
i need to do any sorts really, because this is as basic as it needs to be at
the moment
 

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

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top