I wish a happy 2008 to everyone on this forum.
I just obtained the practise of programming{by the way it seems like a great book...}
and i have the following code for linked list:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Nameval
{
char *name;
int value;
struct Nameval *next; //for the list implementation
}nameval_t;
/*
* newItem:: create a new item from name and value
*/
nameval_t *newItem(char *name, int value)
{
nameval_t *newp;
newp = (nameval_t *) malloc( sizeof nameval_t );
//do error check
newp->name = name;
newp->value = value;
newp->next = NULL;
return newp;
}
/*
* addFront: add newp to front of listp
*/
nameval_t *addFront( nameval_t *listp, nameval_t *newp)
{
newp->next = listp;
return newp;
}
/*
* addEnd: add newp to end of listp
*/
nameval_t *addEnd( nameval_t *listp, nameval_t *newp)
{
nameval_t *p;
if(listp == NULL)
return newp;
//traverse the list to the end....
for(p=listp; p->next !=NULL; p=p->next)
;
p->next = newp;
return listp;
}
/*
* lookUp: sequential search for name in listp
*/
#if 1
nameval_t *lookUp( nameval_t *listp, char *name)
{
for ( ; listp != NULL; listp = listp->next)
//if ( strcmp(name, listp->name) == 0 )
if ( strncmp(name, listp->name, sizeof(listp->name) ) == 0 )
return listp;
return NULL; //no match
}
#endif
int main()
{
nameval_t *listhead = NULL;
//struct Nameval *listhead = NULL;
listhead = addFront( listhead, newItem("hello", 1) );
#if 0
listhead = addEnd( listhead, newItem("this", 2) );
listhead = addEnd( listhead, newItem("is", 3) );
listhead = addEnd( listhead, newItem("a", 4) );
listhead = addEnd( listhead, newItem("test?", 5) );
#endif
nameval_t *temp = lookUp( listhead, "this");
if (temp == NULL)
fprintf(stderr, "The string you look for, doesnt exit\n");
else
fprintf(stderr, "String's value is:: %d\n", temp->value);
return 0;
}
the problem is that i can't find why the compiler complains....
main.c: In function ‘newItem’:
main.c:17: error: expected expression before ‘nameval_t’
also in the beginning i sometimes get this error{i did get from the above code...but after some minor changes the error dissapeared}:
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
i can't find a lot for it on the internet... any ideas what it is about...?
thanks for your time,
nicolas