How do I resolve struct declaration warning?

2

I'll send the code and warnings that GCC is reporting. Should I ignore the warnings?

warnings are related as passing from struct to function 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)
    
asked by anonymous 21.09.2017 / 17:01

1 answer

1
The main problem is that you have created the type Lista and not a struct called Lista , the structure is anonymous, the type is what is named, then it should use the type and not the structure. I enjoyed 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");
        }
    }
}

See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

    
21.09.2017 / 17:29