-
linked list errors
i am running this program
Code:
#include<stdio.h>
struct node
{
int data;
struct node *ptr;
}
int a,i=0;
main()
{
int o;
struct node *tmp,*lob,*pnt;
tmp=(struct node *)malloc(sizeof(struct node));
lob=(struct node *)malloc(sizeof(struct node));
pnt=(struct node *)malloc(sizeof(struct node));
while(1)
{
printf("Whether you want to add data\n"
"1.yes\n"
"2.print\n"
"3.exit :");
scanf("%d",&o);
switch(o)
{
case(1):
{
add();
break;
}
case(2):
{
print();
break;
}
case(3)
exit(1);
}
}
}
add()
{
scanf("%d",&a);
lob->data=a;
lob->ptr=NULL;
tmp->ptr=lob;
tmp=lob;
if(i<1)
{
pnt=tmp;
i++;
}
}
print()
{
while(pnt->data==NULL)
{
printf("%d\n",pnt->data);
pnt=pnt->ptr;
}
i am getting these errors
Code:
linkedlist.c:7: error: two or more data types in declaration specifiers
linkedlist.c:7: error: invalid initializer
linkedlist.c: In function ‘main’:
linkedlist.c:12: warning: incompatible implicit declaration of built-in function ‘malloc’
linkedlist.c:36: error: expected ‘:’ or ‘...’ before ‘exit’
linkedlist.c:36: warning: incompatible implicit declaration of built-in function ‘exit’
linkedlist.c: In function ‘add’:
linkedlist.c:42: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘struct node *’
linkedlist.c:43: error: ‘lob’ undeclared (first use in this function)
linkedlist.c:43: error: (Each undeclared identifier is reported only once
linkedlist.c:43: error: for each function it appears in.)
linkedlist.c:45: error: ‘tmp’ undeclared (first use in this function)
linkedlist.c:47: error: invalid operands to binary < (have ‘struct node’ and ‘int’)
linkedlist.c:49: error: ‘pnt’ undeclared (first use in this function)
linkedlist.c:50: error: wrong type argument to increment
linkedlist.c: In function ‘print’:
linkedlist.c:55: error: ‘pnt’ undeclared (first use in this function)
could anyone please help me what is the error
-
I'll do the first few:
Code:
linkedlist.c:7: error: two or more data types in declaration specifiers
linkedlist.c:7: error: invalid initializer
Probably because you are missing a semi-colon here:
Code:
struct node
{
int data;
struct node *ptr;
};
int a,i=0;
Code:
linkedlist.c: In function ‘main’:
linkedlist.c:12: warning: incompatible implicit declaration of built-in function ‘malloc’
You need to #include <stdlib.h>.
Code:
linkedlist.c:36: error: expected ‘:’ or ‘...’ before ‘exit’
linkedlist.c:36: warning: incompatible implicit declaration of built-in function ‘exit’
Exit() is also in stdlib. I'll let you make an educated guess about the "expected :" here
Now, learn to indent properly please! For your own sake, and everyone elses.
-
Thanks for your help. Here i have one doubt.
Code:
struct node *tmp,*lob,*pnt;
tmp=(struct node *)malloc(sizeof(struct node));
lob=(struct node *)malloc(sizeof(struct node));
pnt=(struct node *)malloc(sizeof(struct node));
why cant we declare struct pointer variables declaration and allocating memory globally?why it needs to come inside main()
if i have to use all the memory allocation for struct pointer variables inside main() means it wont work in display().in this case whether i have to declare again in display()?
-
You can declare variables globally. But you can only run code inside a function, and malloc is a function call and therefore code.
Functions take parameters for a reason, and "this function needs to know about these variables" is that reason.
-
ok fine. now my doubt is if i run that "malloc" code inside main(),whether is it necessary to allocate memory again in display()?