Saturday, May 22, 2010

C Syntax Error Help?

#ifndef __LINKEDLIST_H


#define __LINKEDLIST_H





#define MAX_NAME_LENGTH 25


#define PHONE_NUMBER_LENGTH 12





//entry structure definition


typedef struct {


char firstName[MAX_NAME_LENGTH];


char lastName[MAX_NAME_LENGTH];


char phoneNumber[PHONE_NUMBER_LENGTH];


NODE *previousNode;


NODE *nextNode;


} NODE;





typedef struct {


NODE *firstNode;


NODE *lastNode;


NODE *currentNode;


int counter;


} LINKEDLIST;





bool newentry(LINKEDLIST *myLinkedList, char *firstName, char *lastName, char *phoneNumber);


void displayall(LINKEDLIST myLinkedList);


bool removeentry(LINKEDLIST *myLinkedList, char *phoneNumber);





#endif











Errors


--------


linkedlist.h:12: error: expected specifier-qualifier-list before âNODEâ


linkedlist.h:23: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before ânewentryâ


linkedlist.h:25: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before âremoveentryâ

C Syntax Error Help?
You have to forward declare the type NODE. What happens is that the type NODE does not exist until the declaration is complete. When the compiler hits NODE *previousNode, the compiler doesn't know NODE is a type.





You can declare the name without putting anything inside it. You can create pointers to incomplete types.





** Edit **


I forgot that you cannot forward declare a typedef struct. You have to use struct tags instead. The concept is the same, you just cannot use the typedef name until after the full declaration.





struct tagNODE; // forward declaration here


// now, when the compiler sees struct tagNODE, it knows it will be a struct type


// The compiler will happily make a pointer to it.





typedef struct tagNODE


{


// your stuff here


struct tagNODE *previousNode;


struct tagNODE *nextNode;


} NODE;





// After the declaration, you can use type NODE


No comments:

Post a Comment