Searching trough an enumeration

A

Antoine Junod

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
 
A

August Karlstrom

Antoine said:
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
 
K

Krishanu Debnath

Antoine said:
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
 
A

Antoine Junod

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
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top