473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

checking the contents of an array..

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
6 1666

"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
<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bart Plessers \(artabel\) | last post by:
Hello, My script builds a list of files in a folder. These files are stored in an 2-dimensional array, called "a_files". To speed up the script, I don't want the filelist everytime being read...
1
by: Bart Plessers \(artabel\) | last post by:
Hello, Currently developping a site, where I use some session variables. The site is located at my home computer. Because I have a dynamic IP adress, I use no-ip (www.no-ip.org) to have my...
2
by: CES | last post by:
All, Sorry for the 101 question but I can't figure out how to return all of the contents of an array to another function. I been able to figure out how to return individual elements of the array...
6
by: Deano | last post by:
I think my brain has short-circuited again :) Is this the quickest way to check for the existence of a given value in an array? e.g For i = 0 To rrst.RecordCount If myArray(i) =...
30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
6
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
3
by: iler.ml | last post by:
I am writing code that uses two third-party libraries. They both define same macro OP_ENCRYPT, and luckily for me, they both define it to 0. (In one include, it's '#define OP_ENCRYPT 0', in...
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.