If this is an exam, and you must use a Flexible Array Member, then as I indicated in the comments and as @rici explained in his answer, the purpose of the FAM is to provide a placeholder of a given type
that then allows you to allocate storage for the struct itself plus storage for some number of your FAM type in a single-allocation. The advantage this provides is a single-allocation/single-free rather than separate allocation for the struct and then allocating for some number of your needed type.
(prior to the FAM, there was what was referred to as the struct hack where an array of size 1
was used in its place for much the same purpose)
The type
is critical to how you deal with and allocate for your FAM. In your case your FAM is item *items[];
(an array of pointers to type item
-- Item
in your code) So you allocate for the struct and then X
number of pointers to item
.
To initialize each member of items
, you must assign a valid address to a struct of type item
(or you can separately allocate, copy to the new block, and then assign the starting address for that block to a pointer in items
) In your case, you have an array of struct item
called fruits
. To assign to items
, you must assign the address of each struct to each element in items
(remember you have storage for pointers, not storage for struct item
-- and you must ensure fruits
remains in scope for the duration of your use of basket
)
Putting those pieces together, you could do something similar to the following:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct {
int low, high;
char label[16];
} item;
typedef struct {
size_t length;
item *items[];
} item_coll;
char *find_first_in_range(item_coll *ic, int rlow, int rhigh)
{
for (size_t i = 0; i < ic->length; i++)
if (ic->items[i]->low >= rlow && ic->items[i]->high <= rhigh)
return ic->items[i]->label;
return NULL;
}
int main() {
item fruits[] = {
{10, 20, "Apple"},
{12, 14, "Pear"},
{ 8, 12, "Banana"},
{ 2, 4, "Grape"},
{15, 35, "Watermelon"}
};
size_t nfruits = sizeof fruits/sizeof *fruits; /* avoid magic-numbers */
/* allocate storage for basket + nfruits pointers */
item_coll *basket = malloc (sizeof *basket +
nfruits * sizeof *basket->items);
if (!basket) { /* validate allocation succeeded */
perror ("malloc-basket+5_item_coll");
return 1;
}
basket->length = nfruits; /* assign length */
for (size_t i = 0; i < nfruits; i++) /* assign addresses to structs */
basket->items[i] = &fruits[i];
char *label = find_first_in_range (basket, 12, 15); /* save return */
if (label) /* validate not NULL before printing */
printf ("%s\n", label);
free (basket); /* don't forget to free the memory you allocate */
return 0;
}
(note I have simply used typedefs and removed the struct labels themselves -- it's up to you. Further, you should validate the return from find_first_in_range
is not NULL
before printing.)
Example Use/Output
Also note I've bracket the high/low
range for
$ ./bin/fam_initialization
Pear
Memory Use/Error Check
In any code you write that dynamically allocates memory, you have 2 responsibilities regarding any block of memory allocated: (1) always preserve a pointer to the starting address for the block of memory so, (2) it can be freed when it is no longer needed.
It is imperative that you use a memory error checking program to insure you do not attempt to access memory or write beyond/outside the bounds of your allocated block, attempt to read or base a conditional jump on an uninitialized value, and finally, to confirm that you free all the memory you have allocated.
For Linux valgrind
is the normal choice. There are similar memory checkers for every platform. They are all simple to use, just run your program through it.
$ valgrind ./bin/fam_initialization
==6887== Memcheck, a memory error detector
==6887== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6887== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==6887== Command: ./bin/fam_initialization
==6887==
Pear
==6887==
==6887== HEAP SUMMARY:
==6887== in use at exit: 0 bytes in 0 blocks
==6887== total heap usage: 1 allocs, 1 frees, 48 bytes allocated
==6887==
==6887== All heap blocks were freed -- no leaks are possible
==6887==
==6887== For counts of detected and suppressed errors, rerun with: -v
==6887== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Always confirm that you have freed all memory you have allocated and that there are no memory errors.
Look things over and let me know if you have further questions.