PDA

View Full Version : GCC Hates NULL



ankursethi
October 14th, 2007, 01:29 PM
I've been trying to create a simple linked list implementation. This is one of the functions I wrote. The compiler seems to bitch when I give NULL as the default values to the three arguments.


struct Node* newnode (struct Node* next = NULL, struct Node* prev = NULL, void* data = NULL) {
struct Node* nn = NULL ;
nn = malloc (sizeof(nn)) ;

if (nn != NULL) {
nn->prev = prev ;
nn->next = next ;
nn->data = data ;
}

return nn ;
}


This is the error I get :

linkedlib.c:22: error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token

What's the problem? I'm compiling with gcc -Wall -c FILENAME.c since this file does not contain a main(). I'll link the object file with my executable once this implementation works.

duff
October 14th, 2007, 01:42 PM
C doesn't support default function arguments.

ankursethi
October 14th, 2007, 02:34 PM
Dang. Silly me. So I need to make them all NULL in the beginning of the function? Dang.

Lux Perpetua
October 14th, 2007, 07:52 PM
Dang. Silly me. So I need to make them all NULL in the beginning of the function? Dang.No, that wouldn't work either. The caller is going to have to specify all the arguments.

smartbei
October 14th, 2007, 09:48 PM
You do realize that in the really odd case that GCC actually hated NULL, it only would have been one define away?



#define NULL 0

tageiru
October 15th, 2007, 01:54 AM
You do realize that in the really odd case that GCC actually hated NULL, it only would have been one define away?



#define NULL 0


#define NULL (void *) 0

aks44
October 15th, 2007, 07:10 PM
#define NULL (void *) 0

#define NULL ( (void*) 0 )

:p

tageiru
October 15th, 2007, 07:56 PM
#define NULL ( (void*) 0 )

:p
touché