bsearch and structs, again

  • Thread starter Michiel Rapati-Kekkonen
  • Start date
M

Michiel Rapati-Kekkonen

recently I was put on the right trail in the matter of searching in arrays
of structs.
I got it working, a bit.
Unfortunately, as soon as I want more, I'm stuck again:

it is basically a dictionary I'm trying to make.
I have a huge list of conjugated forms.
One can search that list to find a number, the index in another array, which
refers to the unconjugated word.

If it is not found in that array of forms, I want to search the array of
unconjugated words (it could be an adverb or such).
And here suddenly the 'trick' I have just learned stutters...

*

I made a struct

struct wordforms{
char *wordform; /*the conjugated wordform */
unsigned long orgnr; /*the indexnumber to the array of
unconjugated/not-conjugatable words */
unsigned short formname; /*the index to the name/type of
conjugation */
};

the array would be:

struct wordforms form_list[]={
{"aaaaab", 1, 3},
{"bbaacc", 9012, 4}
...,
{"zzzf", 876, 12}};

Searching would go

void
find_form (char *wordform)
{
struct wordforms target, *result;
target.wordform = wordform;
result = bsearch (&target, form_list, count, sizeof (struct
wordforms), wordf_cmp);
if (result){
print_wordf (result);
}
else
printf ("Couldn't find %s.\n", woordvorm);
}


Searching here goes well
It gives me the index to the original word, stored in another array of
structs

struct dictwords{
char orgword[28];
char meaning[100];
unsigned short wordtype_id;
};

and the array

struct dictwords word_list[]={
{"aaa", "meaning a", 1},
{"abb","meaning b",1},
...
{"zzz", meaning z", 2}};

BUT

When a word is not found I want to search that same word_list array, using
the same type of function as before.
However my compiler informs me that the
'=' : left operand must be l-value.
and it refers to the second line
target2.orgword = orgword
in that adapted function.

If I then change the declaration of the struct into

struct dictwords{
char *orgword;
char meaning[100];
unsigned short wordtype_id;
};

or

struct dictwords{
char *orgword;
char *meaning;
unsigned short wordtype_id;
};

compilation is allright in the first case
but building gives One Big Error:
Internal error during ReadSymbolTable
plus some (as you may have guessed, for me ununderstandable ) tables.

Why do the arrays respond differently to the same type of action?

Your help would be Most appreciated!

Michiel
 
A

Allin Cottrell

Michiel said:
struct dictwords{
char orgword[28];
char meaning[100];
unsigned short wordtype_id;
};

and the array

struct dictwords word_list[]={
{"aaa", "meaning a", 1},
{"abb","meaning b",1},
...
{"zzz", meaning z", 2}};

BUT

When a word is not found I want to search that same word_list array, using
the same type of function as before.
However my compiler informs me that the
'=' : left operand must be l-value.
and it refers to the second line
target2.orgword = orgword

An "l-value" in C is something that can validly put to the left of
the "=" sign, something you can assign to. In this case you are
trying to assign to a char array, orgword[28], which is a member of
a struct dictwords. This can't be done. This is a very elementary
C newbie issue -- check out the comp.lang.c FAQ. If you want to
put new material into a char array you must use strcpy (or similar),
or assign element by element: orgword[0] = 'M', orgword[1] = 'y', and
so on. But in no way can you assign to an array.

Allin Cottrell
 
M

Michiel Rapati-Kekkonen

thanks very much!
The way of the newbie is a long one.
But now at least in principle my program seems to work
and, as you can understand, new questions arrive!

I changed the struct to the pointer variation
and cut a huge part off the arrays used.
Now the complaints have disappeared.
However
I would like to put my wordcollections in arrays.

How big may they be
and can I tell them to be bigger than the default?

The reason that I prefer those arrays over external files
is that I hope to put it once in a small device with a modest processor and
modest RAM and Flash.
So speed is important. And searching in arrays seems to be super fast.

Michiel
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top