How do I resolve struct statement Warning?

Asked

Viewed 499 times

2

I’ll send the code and the warnings that GCC is reporting. I must ignore warnings?

The warnings are related as the passage of struct for the job Inserir();:

#include <stdio.h> 
#include <stdlib.h>

int menu();
int PosValida();
int Inserir();



int Inserir(struct Lista *vet, int pos)
{

}


int PosValida()
{
    int pos;

    do{
        printf("Informe Posição da Estrutura Principal\n");
        scanf("%d", &pos);
            if(pos<0 || pos>10)
                printf("Posição Inválida\n");
    }while(pos<0 || pos>10);
    return pos;

}

typedef struct 
{
    int PosAtual;
    int UltimaPos;
    int *VetAux;

}Lista;

int menu()
{
    int op;
    printf("Digite as opção desejada\n");
    printf("0 - Sair\n");
    printf("1 - Inserir\n");
    scanf("%i", &op);

    return op;
}

int main()
{

    int op;
    int sair = 0;
    int Posvalida;

    Lista lista_principal[10];

    for(int i=0;i<10;i++)
    {
        lista_principal[i].VetAux=NULL;
        lista_principal[i].UltimaPos=0;
        lista_principal[i].PosAtual=0;
    }

    while (!sair){
        op = menu();
        switch (op){
            case 0:{
                sair =1;
                break;
            }
            case 1:{ //inserir
                Posvalida=PosValida();
                Inserir(lista_principal, Posvalida);

                break;
            }
            default:{
                printf("opcao inválida\n");
            }


        }


    }

    return 0;

}
Teste.c:10:20: warning: ‘struct Lista’ declared inside parameter list
 int Inserir(struct Lista *vet, int pos)
                    ^
 Teste.c:10:20: warning: its scope is only this definition or declaration, which is probably not what you want
 Teste.c: In function ‘main’:
 Teste.c:74:25: warning: passing argument 1 of ‘Inserir’ from incompatible pointer type [-Wincompatible-pointer-types]
                 Inserir(lista_principal, Posvalida);
                         ^
 Teste.c:10:5: note: expected ‘struct Lista *’ but argument is of type ‘Lista * {aka struct <anonymous> *}’
 int Inserir(struct Lista *vet, int pos)

1 answer

1


The main problem is that you created the type Lista and not a struct calling for Lista, the structure is anonymous, the type is named, so should use the type and not the structure. I took advantage and improved some things:

#include <stdio.h> 
#include <stdlib.h>

typedef struct {
    int PosAtual;
    int UltimaPos;
    int *VetAux;
} Lista;

void Inserir(Lista *vet, int pos) {} //mudei aqui

int PosValida() {
    int pos;
    do {
        printf("Informe Posição da Estrutura Principal\n");
        scanf("%d", &pos);
            if (pos < 0 || pos > 10)
                printf("Posição Inválida\n");
    } while (pos < 0 || pos > 10);
    return pos;
}

int menu() {
    int op;
    printf("Digite as opção desejada\n");
    printf("0 - Sair\n");
    printf("1 - Inserir\n");
    scanf("%i", &op);
    return op;
}

int main() {
    Lista lista_principal[10];
    for (int i = 0; i < 10; i++) {
        lista_principal[i].VetAux = NULL;
        lista_principal[i].UltimaPos = 0;
        lista_principal[i].PosAtual = 0;
    }
    int opcao = 1;
    while (opcao) {
        opcao = menu();
        switch (opcao) {
            case 0:
                break;
            case 1:
                Inserir(lista_principal, PosValida());
                break;
            default:
                printf("opcao inválida\n");
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • bigown, thanks. As for the struct, I already imagined that this was it, however, without the struct the function gave error. It only solved when I put the struct on top of the other functions, just like you did. Thank you

  • So you have to fix the right mistake and not introduce new ones ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.