unknown type name defined struct - c

I have this header file node.h:
#ifndef NODE_H
#define NODE_H
#include <stdio.h>
#include "symboltable.h"
#include "threeaddress.h"
#include "types.h"
typedef struct NODE NODE;
typedef struct LITERALNODE LITERALNODE;
struct NODE {
INSTRUCTION code;
SYMBOL_INFO *place;
NODE *next;
NODE *previous;
};
struct LITERALNODE {
TYPE_INFO *typeInfo;
int integerValue;
char charValue;
};
LITERALNODE *generateIntegerLiteral(TYPE_INFO *typeInfo, int integerValue);
LITERALNODE *generateCharLiteral(TYPE_INFO *typeInfo, char charValue);
void concatNodes(NODE *node1, NODE *node2);
#endif
And this node.c file
#include "node.h"
#include "types.h"
LITERALNODE *generateIntegerLiteral(TYPE_INFO *typeInfo, int integerValue) {
LITERALNODE *literalNode;
literalNode->typeInfo = typeInfo;
return literalNode;
}
LITERALNODE *generateCharLiteral(TYPE_INFO *typeInfo, char charValue) {
LITERALNODE *literalNode;
literalNode->typeInfo = typeInfo;
return literalNode;
}
void concatNodes(NODE *node1, NODE *node2) {
node1->next = node2;
node2->previous = node1;
}
When I try to compile node.c I get the following errors:
node.c:4:1: error: unknown type name 'LITERALNODE'
LITERALNODE* generateIntegerLiteral(TYPE_INFO* typeInfo, int integerValue) {
^
node.c: In function 'generateIntegerLiteral':
node.c:5:2: error: unknown type name 'LITERALNODE'
LITERALNODE* literalNode;
^
node.c:6:13: error: request for member 'typeInfo' in something not a structure or union
literalNode->typeInfo = typeInfo;
^
node.c: At top level:
node.c:10:1: error: unknown type name 'LITERALNODE'
LITERALNODE* generateCharLiteral(TYPE_INFO* typeInfo, char charValue) {
^
node.c: In function 'generateCharLiteral':
node.c:11:2: error: unknown type name 'LITERALNODE'
LITERALNODE* literalNode;
^
node.c:12:13: error: request for member 'typeInfo' in something not a structure or union
literalNode->typeInfo = typeInfo;
I can't seem to spot my mistake, can anyone help me.
Also Why would the compiler comment on LITERALNODE and not on NODE, since they are constructed in exactly the same way.

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.

C programming: invalid application of ‘sizeof’ to incomplete type ‘Tipo {aka struct ElemSCl}

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;

warning: type defaults to 'int' in declaration of 'root' [-Wimplicit-int]|

#include <stdio.h>
#include <stdlib.h>
struct test{
int data;
struct test *link;
};
struct test *root;
root=(struct test*)malloc(sizeof(struct test));
i get error :
warning: data definition has no type or storage class|
warning: type defaults to 'int' in declaration of 'root' [-Wimplicit-int]|
You cannot call code from global scope in C, use a main()
#include <stdio.h>
#include <stdlib.h>
struct test{
int data;
struct test *link;
};
int main(){
struct test *root;
root=(struct test*)malloc(sizeof(struct test));
// Then write some meaningful code here
// remember to test if root is null and exit if malloc failed
// and finally free your allocated memory
free(root);
}

Link list incompatible pointer type

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));

Why Does Clang See a Type Conflict?

RESOLVED!
Maybe I'm losing my edge, but to me I don't see why Clang is giving me the following error when I compile my linked list file (llist).
error: conflicting types for 'remove'
int remove(struct node *llist, int val);
note: previous declaration is here
extern int remove(const char *__filename) __THROW;
My .h file:
struct node{
int val;
struct node *left, *right;
};
struct node* get(struct node *llist, int i);
int remove(struct node *llist, int val);
struct node* search(int val, struct node *llist);
void deleteList(struct node *llist);
void add(struct node *llist, struct node *toAdd);
My .c file:
#include <stdio.h>
#include <stdlib.h>
#include "llist.h"
int remove(struct node *llist, int val){
struct node *cur = llist->right;
while(cur != llist){
if(cur->val != val)
cur = cur->right;
else{
cur->left->right = cur->right;
cur->right->left = cur->left;
free(cur);
return 1;
}
}
return 0;
}
There is a standard function named remove in stdio.h that has a signature of:
int remove(const char *filename);
Rename your function.
Note: as #R.. points out, the name remove is reserved even if stdio.h is not included.
Let's try and compile this reduced test case:
#include <stdio.h>
struct node;
int remove(struct node *llist, int val);
If we compile this, we get the following notifications:
foo.c:3:5: error: conflicting types for 'remove'
int remove(struct node *llist, int val);
^
/usr/include/stdio.h:261:6: note: previous declaration is here
int remove(const char *);
^
1 error generated.
This tells us pretty clearly that stdio.h has already defined a remove() function, and you need to rename your function.
If you're interested in what that remove() does, you can learn about it here.

Resources