| | |
linked list error
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
the problem is that i can't find why the compiler complains....
also in the beginning i sometimes get this error{i did get from the above code...but after some minor changes the error dissapeared}:
i can't find a lot for it on the internet... any ideas what it is about...?
thanks for your time,
nicolas
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:
c Syntax (Toggle Plain Text)
#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’
•
•
•
•
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
thanks for your time,
nicolas
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
THANK YOU duoas... i 've been looking this code for one hour before i post here trying to find the error.... can i ask something though:
why sizeof nameval_t was wrong?
why sizeof nameval_t was wrong?
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
searching a bit i found this::
so when we use sizeof with a type we must use a parenthesis?
what's the logic behind this decision?
C Syntax (Toggle Plain Text)
/* * C99 * 6.5.3.4.2 * The sizeof operator yields the size (in bytes) of its operand, * which may be an expression or the parenthesized name * of a type. */
so when we use sizeof with a type we must use a parenthesis?
what's the logic behind this decision?
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
My best guess is that the argument is a "type-cast expression" (according to MSDN), which always takes the form:
This makes a semantic difference when lexing/parsing types like
and
.
I'm not too sure though... I've never cared enough to get that deep into it, as it is always legitimate to surround any sizeable expression with parentheses...
(typename)This makes a semantic difference when lexing/parsing types like
struct fooand
int *.
I'm not too sure though... I've never cared enough to get that deep into it, as it is always legitimate to surround any sizeable expression with parentheses...
it seems that this
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token error appears when in the declaration of a functions you miss a ; in the end... Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
![]() |
Similar Threads
- *strcpy and linked list (C#)
- sort linked list help please (C)
- doubly linked list--- help needed1 (C++)
- more linked list printing problems (C)
- Linked List (C)
- Beginner with C++ and goofy run time problem with DBL Linked List (C++)
- error C2447: missing function header (old-style formal list?), what does this mean?! (C++)
- printing a linked list (C++)
- Linked List (C++)
Other Threads in the C Forum
- Previous Thread: whats wrong with my code??
- Next Thread: Adding character to inputbuffer
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






