-
list printing
i'm trying to print out elements in a linked list however it wont compile, apparantly next is undeclared, but i have declared it, what is wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct L {
int number;
struct L *next;
} listelem;
listelem *insert_list(int n, listelem *p) {
listelem *temp = calloc(1,sizeof(listelem));
temp->number = n;
temp->next = p;
return temp;
}
listelem *read_list( void ) {
int n;
listelem *lp = NULL;
scanf("%d", &n);
while (n != 0) {
lp = insert_list(n,lp);
scanf("%d", &n);
}
printf("\n");
return lp;
}
int main( void ) {
listelem *lp = read_list();
listelem *lp next;
for( ; lp ; lp = next ) {
printf( "%c", lp->number );
next = lp->next;
free( lp );
}
return 0;
}
any help is appreciated
-
Code:
for( ; lp ; lp = next ) {
next isn't declared. next isn't a stand alone variable. It's a member of a structure. As such, the actual variable is lp, and next is a member of lp.
Quzah.
-
Code:
int main( void ) {
listelem *lp = read_list();
listelem *lp next; <---------- ?
If you had read the error message the compiler gave you, and gone to the line number it gave you, this should have been pretty obvious(?).
-
so how do i need to adjust it?
-
ive sorted my problem out now, i was being silly....