I'm working on this code and im getting conflicting types on several functions, which i pass pointers to some structures as parameters, and i can't see whats wrong, for example, in a function a set:
cliente *aux = f->inicio;
where cliente is a structure, but when i call it in another function with as:
tratar_doc(aux);
where the signature of it is:
void tratar_doc(cliente *c)
I get this warning: conflicting types for 'tratar_doc' [enabled by default]|
Even though my function takes a pointer of the type cliente and what im passing as an argument is a pointer of the type cliente.
Edit: Here is a code to reproduce the problem, the simplest i could get:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct documento{
int chave;
char nome[50];
struct documento *prox;
}documento;
typedef struct cliente{
int conta;
char nome[50];
char tipo;
struct pilha *doc;
struct cliente *prox;
}cliente;
typedef struct fila{
int tamanho_fila;
struct cliente *inicio;
struct cliente *fim;
}fila;
typedef struct pilha{
int tamanho_doc;
struct documento *primeiro;
struct documento *ultimo;
}pilha;
void atender(fila *f){
cliente *aux = f->inicio;
cliente *aux2 = f->fim;
int i;
for(i = 0; i < ((f->tamanho_fila) - 1); i++){
aux2 = aux2->prox;
}
f->inicio = aux2;
tratar_doc(aux);
free(aux);
}
void tratar_doc(cliente *c){
pilha *aux = c->doc;
}
Ther warning: conflicting types for 'tratar_doc' [enabled by default]|
This type of error can occur without a forward declaration of the function. If there is no forward declaration, the first time the compiler encounters the function, it can assign it a type. When the function is defined, this type may be in conflict of the assigned type.
Place
void tratar_doc(cliente *c);
before any other mention of the function. Often these forward declarations are placed in included header files.
Related
When I try to compile this small snippet of code I get the following errors:
./main.c:25:8: error: incomplete definition of type 'struct ElementoDiLista'
lista->info=10;
~~~~~^
./main.c:12:16: note: forward declaration of 'struct ElementoDiLista'
typedef struct ElementoDiLista* ListaDiElementi;
advice please ....
the code:
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
struct elemento
{
int info;
struct elemento* next;
};
typedef struct elemento ElementoDiLista;
typedef struct ElementoDiLista* ListaDiElementi;
int main(void) {
ElementoDiLista elem;
elem.info=10;
elem.next=NULL;
ListaDiElementi lista;
lista=malloc(sizeof(ElementoDiLista));
lista->info=10;
return 0;
}
I expect that my code works since come from a book.
You do not have complete type struct ElementoDiLista. You have complete type struct elemento and its alias ElementoDiLista.
struct elemento
{
int info;
struct elemento* next;
};
typedef struct elemento ElementoDiLista;
So instead of this typedef definition
typedef struct ElementoDiLista* ListaDiElementi;
you have to write
typedef ElementoDiLista* ListaDiElementi;
The compiler issues an error because in this typedef definition
typedef struct ElementoDiLista* ListaDiElementi;
you introduced a new type specifier struct ElementoDiLista that is an incomplete type and has nothing common with struct elemento nor with its alias ElementoDiLista.
I'm learning, and this was a simple test.
scl.h
#ifndef sclh
#define sclh
typedef struct{
int value; }listnote;
struct ElemSCL {
listnote info;
struct ElemSCL*next;
};
typedef struct ElemSCl Tipo;
typedef Tipo *Mangiato;
void Addscl(Mangiato*scl, int e) ;
#endif
prove.c
#include "scl.h"
#include<stdlib.h>
#include<stdio.h>
//create node
void Addscl (Mangiato *scl, int e) {
Mangiato temp;
temp = *scl;
*scl= (Tipo*) malloc(sizeof(Tipo));
*(scl)->info.value= e;
*(scl)->next = temp;
}
Main.c
#include<stdio.h>
#include<stdlib.h>
#include"scl.h"
int main()
{
Tipo *scl= NULL;
Addscl (&scl,3);
printf("%d", *(scl)->info.value);
}
return 0;
}
I'm getting the following error:
main.c:9:22: error: dereferencing pointer to incomplete type ‘Tipo {aka struct ElemSCl}’
printf(" %d", *(scl)->info.value);
^~
prove.c: In function ‘Addscl’:
prove.c:9:29: error: invalid application of ‘sizeof’ to incomplete type ‘Tipo {aka struct ElemSCl}’
*scl= (Tipo*) malloc(sizeof(Tipo));
^~~~
prove.c:10:7: error: ‘*scl’ is a pointer; did you mean to use ‘->’?
*(scl)->info.value= e;
^~
->
prove.c:11:7: error: ‘*scl’ is a pointer; did you mean to use ‘->’?
*(scl)->next = temp;
^~
->
The expression *(scl)->info.value is the same as *(scl->info.value). I.e. you dereference the value member.
You need (*scl)->info.value to dereference the scl pointer.
As for the sizeof problem, you have
struct ElemSCL { ... };
and
typedef struct ElemSCl Tipo;
Pay close attention to the spelling... You use lower-case l in the typedef, it should be upper-case L:
typedef struct ElemSCL Tipo;
I'm having problem implementing queue in c using linked list. I've been reading so many other questions on it here in stackoverflow and in other websites as well, but I still don't know how to fix this issue of my code yet. Anyone, please, help me?
here, it is my code in c:
#include <stdio.h>
#include <stdlib.h>
struct elem{
int n;
elem *prox;
};
struct fila{
elem *inicio, *fim;
}nop;
void iniciar(fila *d){
d->inicio = (elem*) malloc(sizeof(elem));
d->fim = (elem*) malloc(sizeof(elem));
d->inicio->prox = d->fim;
d->fim->prox = NULL;
}
void inserir(fila *d, int x){
elem *novo = (elem*) malloc(sizeof(elem));
novo->n = x;
novo->fim->prox = NULL;
if(d->inicio->prox == NULL)
d->inicio->prox = novo;
else{
d->fim->prox = novo;
d->fim = novo;
}
}
void deletar(fila *d){
if(d->inicio->prox = NULL)
printf("Lista vazia")
else{
elem *apg;
apg = d->inicio->prox;
d->inicio->prox = apg->prox;
printf("%d", d->n);
free(apg);
}
}
int main(){
int x;
iniciar(&nop);
scanf("%d", &x);
inserir(&nop, x);
deletar(&nop);
return 0;
}
So, the question is pretty vague, however I do see one problme in this, which is that you're getting tags and typedef's confused. For example:
void inserir(fila *d, int x)
should actually be defined as
void inserir(struct fila *d, int x)
because you defined fila as a tag. The other alternative is to declare the structure fila with the typedef keyword (liek the below)
typedef struct elem{
int n;
struct elem *prox;
} element;
typename struct {
element *inicio, *fim;
}fila nop;
Most of the errors in this code are because you use elem and fila as type names. Those types however are not defined. When referencing a struct type, you need to prefix it with the struct keyword.
You can get around this by adding a typedef for each one so that they may be referenced by just the type name:
typedef struct elem elem;
typedef struct fila fila;
Put these lines at the top of your source file after the includes. That will address most of the compilation issues. The rest you should be able to figure out yourself.
I dont understand why assigning 'root' to equal to 'trieu' would be an incompatible pointer :/
#include <stdio.h>
#include <stdlib.h>
struct uniform {
char size;
int number;
struct uniform *ext;
};
struct uniform *trieu;
struct unifrom *root;
int main(void) {
trieu = malloc(sizeof(struct uniform));
root = trieu;
...
trieu = root;
When I compile it with gcc it gives me:
program.c: In function ‘main’:
program.c:15:7: warning: assignment from incompatible pointer type
root = trieu;
^
program.c:57:8: warning: assignment from incompatible pointer type
trieu = root;
It worked before in another program I made with:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ll {
char store;
struct ll *ext;
};
struct ll *trieu;
struct ll *root;
int main(int argc, char* argv[]) {
trieu = malloc(sizeof(struct ll));
root = trieu;
...
You have a typo. Use struct uniform *root; instead of struct unifrom *root;.
First of all when using malloc to allocate a new sturct you need to cast the return pointer.
struct uniform {
char size;
int number;
struct uniform *ext;
}UNIFORM;
trieu = (UNIFORM*)malloc(sizeof(UNIFORM));
When compiling this code with gcc
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct _Nodo
{
unsigned int id_thread;
int id_mutex;
_Nodo *solicita;
_Nodo *asignado;
}Nodo;
I get:
libdrm.c:13: error: expected specifier-qualifier-list before ‘_Nodo’
Why?
Try: struct _Nodo *solicita.
As Andrea has already said, it needs to be struct _Nodo * for both solicita and asignado, i.e.:
typedef struct _Nodo
{
unsigned int id_thread;
int id_mutex;
struct _Nodo *solicita; // <<<
struct _Nodo *asignado; // <<<
} Nodo;
Since you are doing a typedef, anyhow, do
typedef struct Nodo Nodo;
struct Nodo {
unsigned int id_thread;
int id_mutex;
Nodo *solicita;
Nodo *asignado;
};
i.e make a forward declaration of your struct and typedef in one go. The names for them need not be different. Then you may already use the typedef name inside the declaration of the struct.