Thread: Scope Problem? "dereferencing pointer to incomplete type"

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Scope Problem? Solved!

    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);
    }
    Last edited by DuncanD; 04-08-2009 at 11:48 AM. Reason: Problem solved

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Code:
    struct nodestr	
    {
    	struct 	nodestr *nextptr;
    	struct	datastr *dataptr;
    };
    It seems the compiler is complaining because in your code, "struct data" isn't defined anywhere, and thus cannot be referenced. By the looks of it, you want "struct datastr" instead.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Brilliant, thanks very much for your prompt help.

    That's exactly the sort of mistake I thought I'd ruled out, doh!

    Program working now.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM