Results 1 to 8 of 8
Here is my linked list c program i dont know what is the error in this program before posting the error could anyone please tell me what is the error ...
- 05-04-2010 #1Linux User
- Join Date
- Aug 2008
- Location
- Trichy,India
- Posts
- 308
linkedlist c program
Here is my linked list c program i dont know what is the error in this program before posting the error could anyone please tell me what is the error in this?
I am doing my program in GCC. Not in turbo cCode:#include<stdio.h> #include<stdlib> struct node { int data; struct node *ptr; } int a,i=0; 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)); main() { int o; 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; } }Thanks in advance...
- 05-04-2010 #2
Where to start...This should be called within the main function.
Code:tmp=(struct node *)malloc(sizeof(struct node)); lob=(struct node *)malloc(sizeof(struct node)); pnt=(struct node *)malloc(sizeof(struct node));
Make mine Arch Linux
- 05-04-2010 #3Linux User
- Join Date
- Aug 2008
- Location
- Trichy,India
- Posts
- 308
now i am getting these errors
could anyone please help me what is the errorCode: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)
Thanks in advance...
- 05-04-2010 #4
#include<stdlib>
should be
#include<stdlib.h>
That should correct some of the errors.Make mine Arch Linux
- 05-05-2010 #5Linux User
- Join Date
- Aug 2008
- Location
- Trichy,India
- Posts
- 308
still i am getting these errors
Code:linkedlist.c:8: error: two or more data types in declaration specifiers linkedlist.c:8: error: invalid initializer linkedlist.c: In function ‘main’: linkedlist.c:37: error: expected ‘:’ or ‘...’ before ‘exit’ linkedlist.c: In function ‘add’: linkedlist.c:43: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘struct node *’ linkedlist.c:44: error: ‘lob’ undeclared (first use in this function) linkedlist.c:44: error: (Each undeclared identifier is reported only once linkedlist.c:44: error: for each function it appears in.) linkedlist.c:46: error: ‘tmp’ undeclared (first use in this function) linkedlist.c:48: error: invalid operands to binary < (have ‘struct node’ and ‘int’) linkedlist.c:50: error: ‘pnt’ undeclared (first use in this function) linkedlist.c:51: error: wrong type argument to increment linkedlist.c: In function ‘print’: linkedlist.c:56: error: ‘pnt’ undeclared (first use in this function)
Thanks in advance...
- 05-05-2010 #6
Your functions add() and print() should be declared above the main function....not inside of it plus your printf function should only have one string like below:
In your switch statement your mixing up colons and semi-colons.Code:printf("Whether you want to add data\n1.yes\n2.print\n3.exit:");Make mine Arch Linux
- 05-12-2010 #7Just Joined!
- Join Date
- May 2010
- Posts
- 5
As "gerard4143" mentioned, your printf() is wrong, causing problems.
So as your struct is not ending with a semi-colon. That is also creating problems for the datatype "node" used.
Fix them, and then post if any further errors.
- 05-13-2010 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Is this a class exercise, or are you trying to teach yourself C programming? The errors in this example are myriad and serious, but elementary and violate the most basic C programming principals and current C language standards. None of the errors are difficult to fix, but I won't help until I know for sure that this is not school work.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote