GCC Compiling error: expected specifier-qualifier-list - c

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.

Related

incomplete definition of type struct & forward declaration of 'struct ElementoDiLista

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.

"struct" before already-declared struct

I am still new in C. I know you can just use the already-declared struct as new data type such as int, double, etc. However, I encounter a struct written like this:
struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
};
In this struct, the data type of "next" pointer is struct AdjListNode*. What does struct have to do with the already-declared AdjListNode*? Thanks!
What does struct have to do with the already-declared AdjListNode*?
The answer is that the c syntax requires it.
You do not get a type AdjListNode by writing struct AdjListNode { ... };
AdjListNode is a struct tag and you always have to use struct AdjListNode when declaring variables.
See this simple example (without pointer inside the struct):
#include <stdio.h>
struct sSomeName
{
int x;
};
int main(void) {
struct sSomeName var; // OK, variable of type struct sSomeName
struct sSomeName* pVar; // OK, pointer to variable of type struct sSomeName
// sSomeName var2; // ERROR: unknown type name 'sSomeName'
var.x = 5;
pVar = &var;
printf("%d\n", pVar->x);
return 0;
}
So if you want to add a pointer inside the struct, you must write struct sSomeName just as you have to do inside main, i.e. like:
struct sSomeName
{
int x;
struct sSomeName* p;
};
Using typedef
If you want a type named AdjListNode you must use typedef.
A typedef example could look like:
#include <stdio.h>
typedef struct sSomeName sSomeName;
struct sSomeName
{
int x;
sSomeName* p;
};
int main(void) {
sSomeName var;
sSomeName* pVar;
var.x = 5;
var.p = NULL;
pVar = &var;
printf("%d\n", pVar->x);
printf("%p\n", (void*)pVar->p);
return 0;
}
Here with that declaration pointer to structure is declared. This is basically used for implementing linked list or for other data structures like tree.
It does'nt mean that struct is re-declared. It is similar to declare a struct variable.
The structure is created as follows: typedef struct AdjListNode. Example:
#include <stdio.h>
#include <stdlib.h>
typedef struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
}AdjListNode;
typedef struct Nodo{
char *nombre;
int *edad;
struct Nodo *siguiente;
}Nodo;
int main(int argc, char **argv) {
AdjListNode *nodo=malloc(sizeof(AdjListNode));
nodo->dest=1;
nodo->weight=2;
nodo->next=NULL;
printf("Nodo-->dest: %d", nodo->dest);
free(nodo);
}

Error: Unknown type name

I'm new to modular programming and I hope you can help me out :)
So here are my .c .h:
item.h
#define L 31
#define L1 11
typedef struct{
int priority;
char service_type[L];
char client_code[L1];
}*Item;
Item New_client();
item.c
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
Item New_client(){
Item new_c=malloc(sizeof new_c);
printf("Inserire priorita': "); scanf("%d",&new_c->priority);
printf("Inserire servizio: "); scanf("%s",new_c->service_type);
printf("Inserire codice cliente: "); scanf("%s",new_c->client_code);
return new_c;
}
PQ.h
typedef struct Nodo *link;
struct Nodo{
Item item;
link next;
};
void init(link coda);
int empty_(link coda);
link insert_(link h,Item client);
PQ.c
#include <stdio.h>
#include <stdlib.h>
#include "PQ.h"
So as I include PQ.h in PQ.c I get the error: unknown type name 'Item' from CodeBlocks... I can't figure why and what can I do to solve the problem.
You should include item.h in your PQ.h:
#include "item.h"
typedef struct Nodo *link;
struct Nodo{
Item item;
link next;
};
void init(link coda);
int empty_(link coda);
link insert_(link h,Item client);
Update: about error: conflicting types for 'Item'
This becouse preprocessor include item.h twice. You should wrap header with #ifndef __HEADER_NAME__, #define __HEADER_NAME__, #endif combination. See how it can be done for item.h:
#ifndef __ITEM_H__
#define __ITEM_H__
#define L 31
#define L1 11
typedef struct{
int priority;
char service_type[L];
char client_code[L1];
}*Item;
Item New_client();
#endif //__ITEM_H__

Error in the use of linked-lists in C

The function doesnt work. The rest of the code is okay. It finds the maximum in the lists (its also the last element of the list) and then doesn't quit the iteration, instead of that, the program crashes. I got a hint, that suggests, that I have problems wit the us of "()". Maxhelye means the max_pos
typedef short int shorti;
typedef struct szelveny{
int szsorszam;
int lsorszam;
int het;
shorti talalat;
int tnyeremeny;
}szelveny; //szelveny-->ticket, szsorszam-->ticketnumer, //lsorszam-->lotterynumber,het-->week, tnyeremeny-->prize
typedef struct szelveny_element{
szelveny szelveny;
struct szelveny_element *next;
}szelveny_element,*szelveny_pointer;
typedef struct lottozo{
int lsorszam;
shorti het;
int sszelveny;
int nyeremeny;
} lottozo; //lottozo-->lottery
typedef struct lottozo_element{
lottozo lottozo;
struct lottozo_element *next;
} lottozo_element,*lottozo_pointer;
typedef struct het{
shorti het;
lottozo_pointer lhead;
szelveny_pointer szhead;
} het;
typedef struct het_element{
het het;
struct het_element *next;
}het_element,*het_pointer;
szelveny_pointer szelvenyek=0;
lottozo_pointer lottozok=0;
het_pointer hetek=0;
int maxnyeremenyhelye2(int ahet) //maxprizeposition, ahet-->week got as parameter
{
int max=0,maxhelye=-1;
het_pointer hp;
for(hp=hetek;hp!=0;hp=hp->next)
if(hp->het.het==ahet)
{
lottozo_pointer lp;
for(lp=hp->het.lhead;lp!=0;lp=lp->next)
{
if(lp->lottozo.nyeremeny>=max)
{
max=lp->lottozo.nyeremeny;
maxhelye=lp->lottozo.lsorszam;
}
}
return maxhelye;
}
}
Your function int maxnyeremenyhelye2(int ahet) does not always return a value - didn't your compiler warn you about this? The line
return maxhelye;
should be moved down below the following brace.

Unknown conflicting types

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.

Resources