0
I’m working on a list of structures code, where I have a structure called Cad that contains the registration fields, name and salary. And a list containing a field of type struct Cad that stores the fields of struct Cad in list form. So the code goes like this:
#define MAX 5
struct cad
{
int mat;
char nome[30];
float sal;
};
struct lista
{
int fim;
struct cad memo[MAX];
};
typedef struct lista lista;
typedef struct cad cad;
int main()
{ lista l;
cad x;
int opc, pos;
l.fim = -1;
do
{
opc = menu();
switch(opc)
{
case 1: //enfileirar
if(l.fim==MAX-1)
{
printf("Lista Cheia ");
}
else
{
printf("Matricula = ");
scanf("%d", &x.mat);
printf("Nome = ");
fflush(stdin);
gets(x.nome);
printf("Salario = ");
scanf("%f", &x.sal);
inserir(&l, x);
}
break;
case 2: //remover
if(l.fim==-1)
{
printf("Lista vazia ");
}
else
{
x=remover(&l);
printf("\n matricula = %d", x.mat);
printf("\n nome = %s", x.nome);
printf("\n salario = %f", x.sal);
}
break;
However, when creating a function capable of removing the information in one of the positions of struct Cad memo, I think of the error "incompatible types when assigning to type 'Cad' from type 'int'|"
The remove function was like this:
struct cad remover(lista *l)
{
int i;
struct cad aux;
aux = l->memo[0];
for(i=0;i<l->fim; i++)
{
l->memo[i] = l->memo[i+1];
}
l->fim--;
return aux;
}
Could someone please enlighten me as to why this mistake?
Its structure is not a linked list. Usually when it is called "list" it is expected to be a linked list behavior. You presented a vector with a limiting index.
– Jefferson Quesado
On this issue, the duties are declared before the
main
? If they are not, some old C compilers assume that the return isint
– Jefferson Quesado
By the way, don’t use
float
for monetary purposes: https://answall.com/q/219211/64969– Jefferson Quesado
Hello Is this your complete code, or is there another part? If you have and can post I believe it will help a lot to find the error. Because I’ve seen some errors here, you’re coding a list with no pointers, it’ll be hard to get your code right. If you have more post code here, in case you want to create a list that contains, enrollment, name and salary and do the insertion and removal of this information ?
– Rafael Megda