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

Searching trough an enumeration

P: n/a
Hello,

I definitely have a problem to build a clean data structure. I would be
very happy if some of you could help me as well as in the past.

Here is my problem:
-> I have a list of keywords.
-> I would like to link each keyword with a string.
-> I should be able to access the string of a given keyword via that
keyword.
-> I should have the ablity to know if a keyword is in the list of my
keywords

A way to link a string with a keyword is to define an enumeration with
my keywords and index an array of char[] with these keywords.
But that solution does not seem to permit to know if a given keyword is
in my enumeration or not. Am I wrong with that?

I could also define a structure with a field "keyword" and a field
"string". Then I could build an array of that structure. With that
solution I would be able to search trough the table if a given keyword
is in my list of defined keywords with a simple for loop. But in that
case I'm no more able to access directly a string via its keyword. And I
think that is not really smart to build an enumeration with my keywords
to index my structure... too much repetitions.

Does someone has a better idea than me?
Thanks a lot for your help
-AJ

Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Antoine Junod wrote:
Hello,

I definitely have a problem to build a clean data structure. I would be
very happy if some of you could help me as well as in the past.

Here is my problem:
-> I have a list of keywords.
-> I would like to link each keyword with a string.
-> I should be able to access the string of a given keyword via that
keyword.
-> I should have the ablity to know if a keyword is in the list of my
keywords


Perhaps something like:

#include <stdbool.h>

#define KEYWORD_MAX_LENGTH <some-value>
#define KEYWORD_COUNT <some-value>

#define KEYWORD_1 0
#define KEYWORD_2 1
...

struct {
char string[KEYWORD_MAX_LENGTH + 1];
bool present;
} keywords[KEYWORD_COUNT];
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Nov 15 '05 #2

P: n/a
Antoine Junod wrote:
Hello,

[snip]
is in my list of defined keywords with a simple for loop. But in that
case I'm no more able to access directly a string via its keyword. And I
think that is not really smart to build an enumeration with my keywords
to index my structure... too much repetitions.

Does someone has a better idea than me?
Thanks a lot for your help
-AJ


Your question is a bit off-topic here. comp.programming is a better
place to ask this sort of question.

If your keyword set is static, you may use gperf. Try gnu site for
the documentation of gperf.

Krishanu
Nov 15 '05 #3

P: n/a
> > Hello,

[snip]
is in my list of defined keywords with a simple for loop. But in that
case I'm no more able to access directly a string via its keyword. And I
think that is not really smart to build an enumeration with my keywords
to index my structure... too much repetitions.
Does someone has a better idea than me?
Thanks a lot for your help
-AJ

Your question is a bit off-topic here. comp.programming is a better
place to ask this sort of question.


Sorry. I will try to better choose the newsgroup the next time.
If your keyword set is static, you may use gperf. Try gnu site for
the documentation of gperf.


Thanks a lot. It is perfectly what I need :)
-AJ
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.