Originally Posted by
Akeold
Changed to char. But, then how can "initialize" this array?
So, you're saying that you now have this?
Code:
typedef struct{
char grapeSet[5][100];
} tGrapeSet;
/* ... */
tGrapeSet *find_grape(tCellar c, const char *grape)
{
Notice that I used a const char* for grape because if you're trying to find a grape in a cellar, you aren't going to change the grape, so const should be used to avoid accidentally changing the string.
Populating this array is a matter of making sure that you set the initial values of the grape names to be empty strings, and then later when actually populating it, checking that the grape set entry is an empty string before writing to it, e.g.,
Code:
void init_grapeSet(tGrapeSet *grapeSet)
{
size_t i;
for (i = 0; i < sizeof(grapeSet) / sizeof(grapeSet[i]); ++i)
{
grapeSet[i][0] = '\0';
}
}
int append_grape(tGrapeSet *grapeSet, const char *grape)
{
size_t i;
/* This assert is useful if you know that the grape name is supposed to fit
into the grape set. If that is not guaranteed, then this must not be an
assert, but rather you need more complex error handling: */
assert(strlen(grape) < sizeof(grapeSet[0]));
for (i = 0; i < sizeof(grapeSet) / sizeof(grapeSet[i]); ++i)
{
if (grapeSet[i][0] == '\0')
{
strcpy(grapeSet[i], grape);
return 1;
}
}
/* the grape set is full: */
return 0;
}
It would be more time efficient if you recorded the number of entries in use in each grape set, though of course it would be less space efficient since your grape set would then have an additional member.
Your find_grape function likewise needs a loop to loop over the grapes in each grape set to find a match. I would suggest separating it into two functions:
Code:
tGrapeSet *find_grape_in_cellar(const tCellar *cellar, const char *grape);
int check_grape_in_grapeSet(const tGrapeSet *grapeSet, const char *grape);