I am trying to write code to build a stack, but I'm getting compilation errors which don't make sense to me. Here is my stack.h:
struct StackNode {
void* previous;
int value;
};
struct Stack {
StackNode* top;
};
Stack* new_stack () {
StackNode stn = { NULL, 0 };
Stack* st = (Stack*) malloc(sizeof(Stack));
st->top = NULL;
return st;
}
and my main.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main () {
struct Stack* st = new_stack();
return 0;
}
gcc throws these errors:
make (in directory: /home/diego/temp/stack) gcc -g -O2 -std=c99 -c
main.c In file included from main.c:4: Compilation failed. stack.h:10:
error: expected specifier-qualifier-list before ‘StackNode’
stack.h:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’
before ‘*’ token main.c: In function ‘main’: main.c:8: warning:
implicit declaration of function ‘new_stack’ main.c:8: warning:
initialization makes pointer from integer without a cast make: *
[main.o] Error 1
EDIT: I found the error. I forgot to put struct before Stack and StackNode in some lines. Always having struct on those lines solves the issue.
Change:
struct Stack {
StackNode* top;
};
to:
struct Stack {
struct StackNode* top;
};
and anywhere else StackNode or Stack is used and not preceded by struct. If you wish to not specify struct you could use a typedef.
Related
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.
My code:
#include <stdio.h>
node * create(int);
void disp(node *,int);
typedef struct node
{
int data;
struct node *next;
};
node * create(int);
void disp(node *,int);
typedef struct node *head , *p , *c;
int i,n;
int main()
{
printf("\n Enter the number of nodes:");
scanf("%d",&n);
c=create(n);
disp(head,n);
return 0;
}
node * create(int n)
{
head = (node *)malloc(sizeof(node));
scanf("%d", &head->data);
head->next = NULL;
p=head;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p=p->next;
p->next=NULL;
}
return head;
}
void disp(node *head , int n)
{
p=head;
while(p!=NULL)
{
for(i=0;i<n;i++)
{
printf("%d",p->data);
p=p->next;
}
}
}
OUTPUT:
User#Sujata:~/Desktop]$ gcc ll.c
ll.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:4: error: expected ‘)’ before ‘*’ token
ll.c:10: warning: useless storage class specifier in empty declaration
ll.c:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:12: error: expected ‘)’ before ‘*’ token
ll.c: In function ‘main’:
ll.c:19: error: expected identifier or ‘(’ before ‘=’ token
ll.c:20: error: expected expression before ‘head’
ll.c: At top level:
ll.c:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:41: error: expected ‘)’ before ‘*’ token
Got this output . Tried many times using the typedef keyword also. But does not work . Thanks in advance !
.....Your code is messy......
You need to add
typedef struct node node;
before the forward declaration of functions so that the type node is known by the compiler. Also, remove the typedef from
typedef struct node *head , *p , *c;
I have the following code:
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define MAXSIZE 1024
typedef struct stack{
struct TreeNode volum[MAXSIZE];
int top;
}STACK;
STACK st;
st.top = -1;
/* other function definitions such as pop() and push() */
But when I compile it, it gives me error Line 18: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token. Where Line 18 is st.top=-1;
So here I need to initialize the stack, i.e setting the top to -1. I also tried to do it inside the struct: int top=-1; But got the same error. I wonder what the correct way of doing it is. Thanks in advance.
You could try
typedef struct stack {
int top;
struct TreeNode volum[MAXSIZE];
} STACK;
STACK st = { -1 }; // top has to be the first member of the struct
Put st.top = -1; in some function (e.g. main), because you can't do assigning globally.
I'm trying to implement a skew heap in C, but my code doesn't compile. I'm not that experienced in C and never created any type of heap in C. That is why I don't know how to fix it, I'm hoping someone can point me the right direction. I have been reading articles about the skew heap and this is what I got so far using the algorithms I have found online. Thanks in Advance.
typedef struct node
{
int value;
struct node * root;
struct node * leftchild;
struct node * rightchild;
} Node;
struct skewHeap
{
struct node * root;
};
void skewHeapInit (struct skewHeap * sk)
{
sk->root = 0;
}
void skewHeapAdd (struct skewHeap *sk)
{
struct node *n = (struct node *) malloc(sizeof(struct node));
assert(n != 0);
n->value = 0;
n->leftchild = 0;
n->rightchild = 0;
line 185. s->root = skewHeapMerge(s->root, n);
}
void skewHeapRemoveFirst (struct skewHeap *sk)
{
struct node * n = sk->root;
free(n);
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
}
line 196. struct node * skewHeapMerge(struct node *left, struct node *right)
{
struct node *temp = (struct node *) malloc(sizeof(struct node));
if (left == NULL)
return *right;
if (right == NULL)
return *left;
if (left->value < right-> value)
{
temp = left->leftchild;
left->leftchild = skewHeapMerge(left->rightchild, right);
left->rightchild = temp;
return left;
}
else
{
temp = right->rightchild;
right->rightchild = skewHeapMerge(right->leftchild, left);
right->leftchild = temp;
return right;
}
}
These are the compilations errors I'm getting at the moment:
program.c: In function ‘skewHeapAdd’:
program.c:185: warning: implicit declaration of function ‘skewHeapMerge’
program.c:185: warning: assignment makes pointer from integer without a cast
program.c: In function ‘skewHeapRemoveFirst’:
program.c:191: warning: assignment makes pointer from integer without a cast
program.c: At top level:
program.c:196: error: conflicting types for ‘skewHeapMerge’
program.c:185: note: previous implicit declaration of ‘skewHeapMerge’ was here
program.c: In function ‘skewHeapMerge’:
program.c:202: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
program.c:205: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
Regarding the compiler errors,
program.c: In function ‘skewHeapAdd’:
program.c:185: warning: implicit declaration of function ‘skewHeapMerge’
program.c:185: warning: assignment makes pointer from integer without a cast
tells you that no prototype of skewHeapMerge is in scope where skewHeapAdd is defined, hence (the compiler apparently operates in C89 mode, but thankfully warns about it), the compiler supposes an implicit declaration with return type int for skewHeapMerge.
Add a header file with prototypes for all your functions, and #include that in all *.c files where these functions are used or defined, so that the compiler knows the types of the functions.
program.c: In function ‘skewHeapRemoveFirst’:
program.c:191: warning: assignment makes pointer from integer without a cast
that should be the line
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
where sk->root is a struct node*, but due to the implicit declaration of skewHeapMerge, that is assumed to return an int.
program.c: At top level:
program.c:196: error: conflicting types for ‘skewHeapMerge’
program.c:185: note: previous implicit declaration of ‘skewHeapMerge’ was here
here the compiler finds that the definition of skewHeapMerge gives a type conflicting with the one from the implicit declaration.
program.c: In function ‘skewHeapMerge’:
program.c:202: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
program.c:205: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
That is for the lines
if (left == NULL)
return *right;
if (right == NULL)
return *left;
where you ought to return right resp. left instead of *right resp. *left (I overlooked that at first).
You have a mistake in skewHeapRemoveFirst
void skewHeapRemoveFirst (struct skewHeap *sk)
{
struct node * n = sk->root;
free(n);
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
}
where you use n after you freed it. You have to exchange the last two lines in that function.
And in skewHeapMerge
struct node * skewHeapMerge(struct node *left, struct node *right)
{
struct node *temp = (struct node *) malloc(sizeof(struct node));
if (left == NULL)
return *right;
if (right == NULL)
return *left;
you are leaking memory. Remove the allocation, since if temp is used at all, you assign either left->leftchild or right->rightchild to it.
Here is a small program I wrote, (I am still writing it), however till this point the program on compilation should not be giving any error as per my understanding.
#include <stdio.h>
#include <stdlib.h>
struct node t1 {
int data;
struct node *next, *prev;
};
struct node *root;
root = NULL;
int main()
{
int i, j, choice, count;
printf("enter choice\n");
scanf("%d", &choice);
count = 0;
while (choice == 1) {
printf("enter a data element");
scanf("%d", &j);
count++;
}
}
void push()
{
}
void pop()
{
}
The error I get is
cc linklist.c
linklist.c:3:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
linklist.c:8:1: warning: data definition has no type or storage class [enabled by default]
linklist.c:8:1: error: conflicting types for ‘root’
linklist.c:7:14: note: previous declaration of ‘root’ was here
linklist.c:8:8: warning: initialization makes integer from pointer without a cast [enabled by default]
I use gcc and Ubuntu 11.04.
What is the reason that upon compiling the code I get above warning.
struct node *root;
root = NULL;
You can't assign like that outside a function. Drop the root = NULL since it's implicit for objects with static storage (such as global variables).
EDIT
As spotted by Tom Dignan the struct declaration is also wrong:
struct node t1 { ... };
^^
You can't put a statement like root = NULL; at top-level (outside of any function). Do
struct node *root = NULL;
(The = NULL part is actually optional; a global or static pointer is automatically null.)
For one, you have an assignment statement outside of main or a function.
root = NULL;
I have not tried anything else.
struct node t1 {
int data;
struct node *next, *prev;
};
You want to create alias for struct node. It shoud be:
typedef struct node { /* typedef! */
int data;
struct node *next, *prev;
}t1; /* alternative name go here */