linked list error

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

linked list error

 
0
  #1
Jan 1st, 2008
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:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Nameval
  6. {
  7. char *name;
  8. int value;
  9. struct Nameval *next; //for the list implementation
  10. }nameval_t;
  11.  
  12. /*
  13.  * newItem:: create a new item from name and value
  14.  */
  15. nameval_t *newItem(char *name, int value)
  16. {
  17. nameval_t *newp;
  18.  
  19. newp = (nameval_t *) malloc( sizeof nameval_t );
  20. //do error check
  21.  
  22. newp->name = name;
  23. newp->value = value;
  24. newp->next = NULL;
  25.  
  26. return newp;
  27. }
  28.  
  29. /*
  30.  * addFront: add newp to front of listp
  31.  */
  32. nameval_t *addFront( nameval_t *listp, nameval_t *newp)
  33. {
  34. newp->next = listp;
  35. return newp;
  36. }
  37.  
  38. /*
  39.  * addEnd: add newp to end of listp
  40.  */
  41. nameval_t *addEnd( nameval_t *listp, nameval_t *newp)
  42. {
  43. nameval_t *p;
  44.  
  45. if(listp == NULL)
  46. return newp;
  47.  
  48. //traverse the list to the end....
  49. for(p=listp; p->next !=NULL; p=p->next)
  50. ;
  51.  
  52. p->next = newp;
  53. return listp;
  54. }
  55.  
  56. /*
  57.  * lookUp: sequential search for name in listp
  58.  */
  59. #if 1
  60. nameval_t *lookUp( nameval_t *listp, char *name)
  61. {
  62.  
  63. for ( ; listp != NULL; listp = listp->next)
  64. //if ( strcmp(name, listp->name) == 0 )
  65. if ( strncmp(name, listp->name, sizeof(listp->name) ) == 0 )
  66. return listp;
  67.  
  68. return NULL; //no match
  69.  
  70. }
  71. #endif
  72. int main()
  73. {
  74. nameval_t *listhead = NULL;
  75. //struct Nameval *listhead = NULL;
  76.  
  77.  
  78. listhead = addFront( listhead, newItem("hello", 1) );
  79. #if 0
  80. listhead = addEnd( listhead, newItem("this", 2) );
  81. listhead = addEnd( listhead, newItem("is", 3) );
  82. listhead = addEnd( listhead, newItem("a", 4) );
  83. listhead = addEnd( listhead, newItem("test?", 5) );
  84. #endif
  85. nameval_t *temp = lookUp( listhead, "this");
  86. if (temp == NULL)
  87. fprintf(stderr, "The string you look for, doesnt exit\n");
  88. else
  89. fprintf(stderr, "String's value is:: %d\n", temp->value);
  90.  
  91. return 0;
  92. }

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
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"
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: linked list error

 
1
  #2
Jan 1st, 2008
Line 19 should read
newp = (nameval_t *) malloc( sizeof( nameval_t ) );

This is why I recommend just always using parentheses with sizeof. It always works with parens, but makes a difference without...
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: linked list error

 
0
  #3
Jan 1st, 2008
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?
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"
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: linked list error

 
0
  #4
Jan 1st, 2008
searching a bit i found this::

  1. /*
  2. * C99
  3. * 6.5.3.4.2
  4. * The sizeof operator yields the size (in bytes) of its operand,
  5. * which may be an expression or the parenthesized name
  6. * of a type.
  7. */

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"
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: linked list error

 
0
  #5
Jan 1st, 2008
My best guess is that the argument is a "type-cast expression" (according to MSDN), which always takes the form:
(typename)

This makes a semantic difference when lexing/parsing types like
struct foo
and
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: linked list error

 
0
  #6
Jan 5th, 2008
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"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC