House
House

Reputation: 1

troubles finding a specific string in a structure

I'm trying to implement the following function but my code is not functional. Any ideas why?

int *colecao_pesquisa_nome(colecao *c, const char *nomep, int *tam);

Return an indices array referring to the position of all plants that present the full scientific name or any of the local names equal to nomep. Returns by reference the array’s size (tam). If it’s unsuccessful, the function should return NULL.

int *colecao_pesquisa_nome(colecao *c, const char *nomep, int *tam)
{   if(c==NULL || nomep==NULL)return NULL;
    long i=0,a,z=0,y=0;
    int *indice=NULL;
    
    for (i = 0; i < c->tamanho; i++)
    {
        if (strstr(c->plantas[i]->nome_cientifico, nomep)!= NULL)
        {
            indice=(int*)realloc(indice,sizeof(int)*(z+1));
            indice[z]=i;
             z=z+1;
        }
    
        if(strstr(c->plantas[i]->nome_cientifico, nomep) == NULL){
            for(a=0;a<c->plantas[i]->n_alcunhas;a++){;
               if (strstr(c->plantas[i]->alcunhas[a], nomep) != NULL){
                   indice=(int*)realloc(indice,sizeof(int)*(z+1));
                   indice[z]=i;
                   z=z+1;
                   break;
                }
            }  
        }
    }
    *tam=z;
    return indice;
}

Upvotes: 0

Views: 41

Answers (0)

Related Questions