initializing error in while loop - c

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 */

Related

Linked list code below gives interesting errors,can you check that?

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *root = (node*)malloc(sizeof(struct node));
*root->next = NULL;
void print (node*abc)
{
while(abc!=NULL)
{
printf ("%d",abc->data);
abc = abc->next;
}
}
void addTail (node*abc)
{
while (abc->next!=NULL);
abc->next = ( node*)malloc(sizeof(struct node));
}
int main ()
{
printf ("dd");
}
The errors i get here are:
7 22 [Error] 'node' undeclared here (not in a function)
7 27 [Error] expected expression before ')' token
8 5 [Error] expected '=', ',', ';', 'asm' or 'attribute' before '->' token
In function 'addTail':
23 20 [Error] expected expression before ')' token
In function 'addHead':
28 28 [Error] expected expression before ')' token
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
created type struct node
struct node *root = (node*)malloc(sizeof(struct node));
You cannot have code outside a function. The above line does not belong here, hanging out of any function like that.
Besides that, it tries to convert the return value of the malloc() function (of void* type) to a pointer to a type that does not exist (type node does not exist; type struct node exists).
And also, it is, at best redundant and at worse an error, wrong to cast the return value of malloc() in C.
*root->next = NULL;
Again, a statement hanging out of any function. Illegal in C.
void print (node*abc)
Type node does not exist, you can't use pointers to types that do not exist.
{
while(abc!=NULL)
{
printf ("%d",abc->data);
abc = abc->next;
}
}
void addTail (node*abc)
Type node does not exist, you can't use pointers to types that do not exist.
{
while (abc->next!=NULL);
abc->next = ( node*)malloc(sizeof(struct node));
}
int main ()
{
printf ("dd");
}
Have fun!

Assign value to a global variable in c causes error

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.

C implementation of skew heap

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.

'list' undeclared (first use in this function)

I have below code.
I am getting error as 'list' undeclared (first use in this function).
Please help me
#include <stdio.h>
#include <stdlib.h>
struct list{
int data;
struct list *next;
};
typedef struct list *head;
int main()
{
struct list *start;
int i;
start = (list *) malloc(sizeof(struct list));
printf("\nEnter the data : \n");
scanf("%d", &i);
start->data = i;
start->next = NULL;
while(list->next != NULL)
{
printf("%d ", list->data);
list = list->next;
}
return 0;
}
You're using the type list instead of variable name start. Proper code:
while (start->next != NULL)
{
start = start->next;
// etc.
}
Don't cast the return type of malloc - there's no benefit of it and in this case you did it wrong!
start = (list *) malloc(sizeof(struct list));
should be
start = malloc(sizeof(struct list));
the type list * doesn't exist; you meant struct list *.
You can make it even more safe by writing
start = malloc(sizeof(*start));
this way you automatically malloc enough bytes for the (pointer) type of start, which is useful when you later change the type of start - the malloc call doesn't change a bit.
The
start = (list *) malloc(sizeof(struct list));
includes an unnecessary typecast. Just do
start = malloc(sizeof(struct list));
However, your code has more trouble than this. I can answer your question best by asking a question of my own: in your mind, is list a type or an object?
If you answer this question, one suspects that you can fix your code. Good luck.
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
int data;
struct list *next;
} list;
typedef struct list *head;
int main()
{
struct list *start;
int i;
start = (list *) malloc(sizeof(struct list));
printf("\nEnter the data : \n");
scanf("%d", &i);
start->data = i;
start->next = NULL;
while(start->next != NULL)
{
start = start->next;
}
return 0;
}
you can define the type (list *)
Your variable is named « start » and you called it « list ».
start = (struct list *) malloc ...
You've missed struct in casting.
Which is not neccessary at all as Anthales pointed out.
start = malloc ...
One problem you had is you were re-using struct in declaring your struct pointer after you had created a typedef, struct list *start;. Also the struct and typedef cannot have the same name. You get this:
cc -Wall test.c -o test
test.c: In function ‘main’:
test.c:13: error: ‘list_t’ undeclared (first use in this function)
test.c:13: error: (Each undeclared identifier is reported only once
test.c:13: error: for each function it appears in.)
test.c:13: error: ‘start’ undeclared (first use in this function)
test.c:13: error: ‘cur’ undeclared (first use in this function)
test.c:13: warning: left-hand operand of comma expression has no effect
test.c:16: error: expected expression before ‘)’ token
You can choose to use struct list everywhere and skip making using typedef. Use of typedef simplifies how your code reads as noted here: http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29#typedef
I've rewritten what you have just so I could compile it and understand it a little better, and so I could put some data into one node. I remember the whole struct typedef concept taking a little time to sink in, when I was learning C. So, don't give up.
#include <stdio.h>
#include <stdlib.h>
struct list {
int data;
struct list *next;
};
typedef struct list list_t;
int main()
{
list_t *start, *cur;
int i;
start = (list_t *) malloc(sizeof(list_t));
if (NULL != start)
{
cur = start; /* Preserve list head, and assign to cur for list trarversal. */
printf("\nEnter the data : ");
scanf("%d", &i);
cur->data = i;
cur->next = NULL;
cur = start;
while(cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
else
{
printf("Malloc failed. Program ending.");
}
return 0;
}

points as function parameters C

this is probably a simple solution I am not that familiar with C just trying to port my java data structure assignments to C.
this is the error i am getting:
test.c:4: error: expected ‘)’ before ‘*’ token
test.c:11: error: expected ‘)’ before ‘*’ token
#include <stdio.h>
#include <stdlib.h>
void to_screen(NODE *cur){
while(cur->next != NULL){
printf("%d\n", cur->data);
cur = cur->next;
}
}
void add_first(NODE *head, int data){
NODE *cur;
int i;
for(i=0; i<10; i++){
cur = malloc(sizeof(NODE));
cur->data = data;
cur->next = (*head).next;
head->next = cur;
}
}
typedef struct node{
int data;
struct element *next;
}NODE;
int main(){
int i;
NODE *head;
for(i=0; i<10; i++){
add_first(head, i);
}
to_screen(head);
}
You need to move the definition of your struct above the to_screen function. The compiler is saying that it doesn't know what NODE is.
You need to define NODE before it is used. Move the definition to the top.
You need to move this block to the top as 2 other answers recommend.
typedef struct node{
int data;
struct element *next;
}NODE;
You may ask for the reason. The reason is that C language specification is not like Java. So it kinds of compiling from the top to the bottom. So, if it see something undefined, it will look for definition above that point, and if it sees the definition it gets it. It doesn't look below the line of code

Resources