Hello,
This is my first post to this board. I'm writing code to create a linked list (eventually for use as a symbol table with lex), its not finished yet but I have written a little "test" in my main function to make sure there's no problems as I go along.... except there is:
Basically I can't seem to access the data held at the first and only node I have created. The problem manifests itself as an error at compile time: "dereferencing pointer to incomplete type" on the line indicated by !!!ERROR HERE!!!. I have read various explanations of the message, mostly relating to peoples struct definitions either not existing or being within functions, I have posted my whole code so far, so that you can see that this isn't the case in my program.
Many thanks for any help you may be able to give.
Kind regards
Duncan
Code:
#include <stdio.h>
#include <stdlib.h>
/* macro to allocate storage for a new object of type x
returns pointer to object */
#define NEW( x ) (x *)malloc( sizeof( x ))
/* defines data structure */
struct datastr
{
int id;
char *type;
char *value;
};
/* defines node structure */
struct nodestr
{
struct nodestr *nextptr;
struct data *dataptr;
};
/* defines root structure */
struct rootstr
{
int numelts;
struct nodestr *headptr;
struct nodestr *tailptr;
};
/* function to create root of linked list, pointers initialised
to NULL since there are no nodes, num initialised to 0.
Returns pointer to root */
struct rootstr *make_root( void )
{
struct rootstr *rootptr;
if((rootptr = NEW(struct rootstr)) != NULL )
{
rootptr->numelts = 0;
rootptr->headptr = NULL;
rootptr->tailptr = NULL;
}
return rootptr;
}
struct nodestr *make_node( void *data )
{
struct nodestr *nodeptr;
if((nodeptr = NEW(struct nodestr)) != NULL )
{
nodeptr->nextptr = NULL;
nodeptr->dataptr = data;
}
return nodeptr;
}
main( void )
{
struct rootstr *rootptr;
rootptr = make_root();
struct datastr *DATAptr;
DATAptr = NEW(struct datastr);
DATAptr->id = 11;
DATAptr->type = "VAR";
DATAptr->value = "_testvar";
rootptr->numelts = 1;
rootptr->headptr = make_node(DATAptr);
int idval = rootptr->headptr->dataptr->id !!!!!ERROR HERE!!!!!
printf("token id: %d \n", idval);
}