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

 * 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

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) );
	nameval_t *temp = lookUp( listhead, "this");
	if (temp == NULL)
		fprintf(stderr, "The string you look for, doesnt exit\n");
		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,

Recommended Answers

Line 19 should read
[inlinecode]newp = (nameval_t *) malloc( sizeof( nameval_t ) );[/inlinecode]

This is why I recommend just [I]always[/I] using parentheses with [B]sizeof[/B]. It always works with parens, but makes a difference without...

Jump to Post

All 5 Replies

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...

commented: thanks for your help!! :) +2

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?

searching a bit i found this::

	 * C99
	 * 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?

My best guess is that the argument is a "type-cast expression" (according to MSDN), which always takes the form: [B]([/B][I]typename[/I][B])[/B] 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...

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.