Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a read_list function in C

Status
Not open for further replies.

dgboult

Programmer
Apr 29, 2001
7
US
Hello,
I'am having trouble with my read_list function in my header file. I know every thing else works, because when I don't use it the program runs fine. What is wrong with it!!!
This is how I'am calling it:

#include <stdio.h>
#include &quot;functionlist.h&quot;

main()
{
list List_a;

printf(&quot;Enter a list 1: &quot;);

List_a = read_list();

List_a = cons('X',List_a);

printf(&quot;%c\n&quot;, car(cdr(List_a)));
printf(&quot;%c\n&quot;, car(cdr(cdr(List_a))));

return 0;
}

/*Here is the header file*/

#include <stdlib.h>

typedef char data;
typedef struct node{
data info;
struct node *next;
}list_node;
typedef list_node list_head;
typedef list_head *list;

data car(list L)
{
return L->next->info;
}

list nullist(void)
{
list L;
L=(list_head*)malloc(sizeof(list_head));
L->next=NULL;
return L;
}

list cdr(list L)
{
list_head *cdr_head;
cdr_head=(list_head*) malloc(sizeof(list_head));
cdr_head->next=L->next->next;
return (cdr_head);
}

list cons(data X, list L)
{
list_node *new_node;
list_head *cons_head;
new_node=(list_node*)malloc(sizeof(list_node));
cons_head=(list_head*)malloc(sizeof(list_node));
new_node->info=X;
cons_head->next=new_node;
new_node->next=L->next;
return (cons_head);
}

list read_list(void) /* here is the problem area */
{
char x;
list L;
x = getchar();

if(x == ')');
return nullist();
else if(isalpha() == 0)
return read_list();
else
return (cons(x,read_list()));
}

/* Thanks!!! */
 
I think that error u are getting during compilation is :
$>gcc -g -o functionlist functionlist.c
In file included from functionlist.c:2:
functionlist.h: In function `read_list':
functionlist.h:52: parse error before else'
$>

To my knowledge this error is because of the unintentional ';' in the line &quot;if(x == ')'); &quot; .

If you remove this semicolon then every thing works fine.

When I remove it from ur header file, the compilation was uccessful and upon execution got the following result:
$> ./functionlist
Enter a list 1: (a b)
a
b

Hope this solves your problem

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top