This is the complete function:
Code:
struct libri
{
int code;
char title[MAX_TITLE_LEN];
char des[MAX_DES_LEN];
int next;
}
struct libri *start = 0;
struct libri ***search(const char title[])
{
struct libri **books = (struct libri**) malloc(sizeof(struct libri*));
if(!books)
{
puts("Ricerca interrotta");
}
books[0] = start;
struct libri ***point;
struct libri *a = (struct libri*)malloc(sizeof(struct libri));
if(!a)
{
puts("Ricerca interrotta");
}
int s = 1;
for(a = start; a; a = a->next)
{
if(strstr(a->title, title))
{
books = (struct libri **) realloc(books, sizeof(struct comp*) * (s + 1));
if(!books)
{
puts("Ricerca non completata");
goto ret;
}
point = &books;
(*point)[s] = a;
s++;
}
}
(*point)[0]->code = s - 1;
ret:
return point;
}
I solved the previous problems, but two remain:
- The books of the vector element 0 is equal to the element 1 (noticed when running the program)
- If you start searching an empty database the statement "(* point) [0] -> code = s - 1," returns error.