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

Dani AI

Generated

Quick summary and practical fixes.

As pointed out, the immediate compile error came from using sizeof with a type name without the required parentheses — the compiler expects an expression when you omit them. A robust pattern that avoids repeating the type and this class of mistakes is to size by the dereferenced pointer instead of the type name; for example:

newp = malloc(sizeof *newp);

Also, in plain C you do not need to cast malloc's return; omitting the cast prevents some subtle problems if you forget the stdlib.h prototype.

The other message you saw — "expected '=', ',', ';', 'asm' or 'attribute' before '{' token" — almost always means there was a syntax error earlier (commonly a missing semicolon after a struct/typedef or a stray macro). As discovered, a missing ; will produce that. Helpful checks: compile with high warnings and syntax-only checks, and inspect the file as preprocessed output:

gcc -std=c99 -Wall -Wextra -fsyntax-only file.c
gcc -E file.c > file.i

A couple of code-quality notes not covered in the thread: using sizeof on a char * (e.g., sizeof(listp->name)) returns the pointer size, not the string length — that makes strncmp comparisons incorrect. Use strcmp, or strncmp with an explicit string-length limit (computed with strlen or a compile-time max). If the list stores copies of names, allocate them (strdup) and free them when removing nodes to avoid dangling pointers or leaks.

Short checklist: fix the sizeof usage (or adopt sizeof *ptr), turn on -Wall -Wextra, check the source line before the reported error for missing semicolons/braces, correct the string-compare logic, and ensure proper ownership/freeing of allocated strings. For reference on sizeof behavior see the operator description on cppreference: sizeof operator.

Recommended Answers

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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.