By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

checking the contents of an array..

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Advocated" <......@......com> wrote in message
news:b1******************@newsfep4-glfd.server.ntli.net...
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
Nov 14 '05 #2

P: n/a
<snip>
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


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?
Nov 14 '05 #3

P: n/a
Advocated wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #4

P: n/a
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.

--
Für Google, Tux und GPL!
Nov 14 '05 #5

P: n/a
Alexander Bartolich wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #6

P: n/a
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
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.