Thread: Problems with a Dinamic Queue:

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Problems with a Dinamic Queue:

    I was trying to create a dinamic queue, but I'm having some problem taking the idea of how C handles the pointers, I can't even compile this code it says :

    queue.c: In function `enqueue':
    queue.c:75: error: dereferencing pointer to incomplete type
    queue.c:76: error: dereferencing pointer to incomplete type
    queue.c:81: error: dereferencing pointer to incomplete type
    queue.c: In function `dequeue':
    queue.c:93: error: dereferencing pointer to incomplete type
    queue.c:95: error: dereferencing pointer to incomplete type
    queue.c: In function `printQueue':
    queue.c:112: error: dereferencing pointer to incomplete type
    queue.c:113: error: dereferencing pointer to incomplete type



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct queueNode {
    	char data;
    	struct queueNode *nextPtr;
    };
    
    typedef struct queueNode QUEUENODE;
    typedef struct QUEUENODE *QUEUENODEPTR;
    	
    /*Functions Prototipes*/
    
    void printQueue(QUEUENODEPTR);
    int isEmpty(QUEUENODEPTR);
    char dequeue(QUEUENODEPTR *, QUEUENODEPTR *);
    void enqueue(QUEUENODEPTR *, QUEUENODEPTR *, char);
    void instructions(void);
    
    int main(){
    	QUEUENODEPTR headPtr = NULL, tailPtr = NULL;
    	int choice;
    	char item;
    	
    	instructions();
    	printf("?");
    	scanf("%d", &choice);
    		while (choice != 3){
    			switch (choice){
    				case 1:
    					printf("Give me a char: ");
    					scanf("\n%c", &item);
    					enqueue(&headPtr, &tailPtr, item);
    				printQueue(headPtr);
    				break;
    				
    				case 2:
    					if (!isEmpty(headPtr)){
    						item = dequeue(&headPtr, &tailPtr);
    						printf("%c was successufully remove: ", item);
    					}
    				printQueue(headPtr);
    				break;
    			
    				default:
    					printf("Invalid Choice\n\n");
    					instructions();
    				break;	
    			}
    			
    			
    			printf("?");
    			scanf("%d",choice);
    			
    		}
    	printf("Exit\n");
    	return 0;
    }
    
    void instructions( void ){
    	printf("Make a choice:\n"
    		   "1 to add an item.\n"
    		   "2 to remove an item.\n"
    	 	   "3 to exit\n");
    }
    
    void enqueue(QUEUENODEPTR *headPtr, QUEUENODEPTR *tailPtr, char value)
    {
    	
    	QUEUENODEPTR newPtr;
    	
    	newPtr = malloc(sizeof(QUEUENODE));
    		
    	if (newPtr != NULL){
    			newPtr->data = value;
    			newPtr->nextPtr = NULL;
    			
    			if (isEmpty(*headPtr))
    				*headPtr = newPtr;
    			else
    				(*tailPtr)->nextPtr = newPtr;
    			
    			*tailPtr = newPtr;
    	}
    	else
    		printf("%c was not inserted. There is no enought memory\n", value);
    }
    
    char dequeue(QUEUENODEPTR *headPtr, QUEUENODEPTR *tailPtr){
    	char value;
    	QUEUENODEPTR tempPtr;
    	
    	value = (*headPtr)->data;
    	tempPtr = *headPtr;
    	*headPtr = (*headPtr)->nextPtr;
    		if (*headPtr == NULL)
    			*tailPtr = NULL;
    	free(tempPtr);
    	return value;
    }
    
    int isEmpty(QUEUENODEPTR headPtr){
    	return headPtr == NULL;
    }
    
    void printQueue(QUEUENODEPTR currentPtr){
    	if (currentPtr == NULL)
    		printf("The Queue is Empty");
    		else {
    			printf("The queue is: \n");
    				while(currentPtr != NULL){
    					printf("%c --> ",currentPtr->data);
    					currentPtr = currentPtr->nextPtr;
    				}
    		printf("NULL\n\n");
    		}
    }
    So should I try to make a cast os something like this?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Change this
    typedef struct QUEUENODE *QUEUENODEPTR;
    to this
    typedef struct queueNode *QUEUENODEPTR;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Hammer
    Change this
    typedef struct QUEUENODE *QUEUENODEPTR;
    to this
    typedef struct queueNode *QUEUENODEPTR;
    Thanks a lot, it worked, but I don't get it, because if I do

    typedef struct queueNode QUEUENODE;

    Ain't I creating an structure of the kind queueNode called QUEUENODE?

    so why can't I refer to create my *QUEUENODEPTR?
    Well I don't know if the question is clear, if don't please infom I will try to explain bettter.

    Another problem, after making the changes you suggested I'm having segmentation fault on the second time I try to insert something on the queue. Also segmentation fault when trying to remove.
    Well I can be making some mistake on C but I've reviewed the code I can't find any BIG logical mistake, but maybe there is some... Oki I found it it was an syntax error I was doing scanf("%d",choice) instead of scanf("%d",&choice) but I still unable to understand why I had to rename the typedef.
    Last edited by Maragato; 07-28-2004 at 02:07 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>typedef struct QUEUENODE *QUEUENODEPTR;
    This is wrong, because QUEUENODE is already a struct from your earlier typedef (technically its not, but that's close enough for now). You could have also used this:
    >>typedef QUEUENODE *QUEUENODEPTR;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Thumbs up

    Quote Originally Posted by Hammer
    >>typedef struct QUEUENODE *QUEUENODEPTR;
    This is wrong, because QUEUENODE is already a struct from your earlier typedef (technically its not, but that's close enough for now). You could have also used this:
    >>typedef QUEUENODE *QUEUENODEPTR;
    Now I got it! Thx a lot I see I making a kind of double definition... Oki I saw, not fully understood but Igot it for now thx a lot.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, now you got that bit, I can explain further.
    >>typedef struct QUEUENODE *QUEUENODEPTR;
    In this context, QUEUENODEPTR is a typedef for a pointer to a structure of type QUEUENODE. You haven't yet told the compiler what a struct QUEUENODE looks like, but that's OK for now.... until you go like this:
    >>QUEUENODEPTR newPtr;
    >>newPtr->data = value;
    Now you're trying to access a member of a structure that you haven't yet defined, hence the compiler says: dereferencing pointer to incomplete type
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Hammer
    OK, now you got that bit, I can explain further.
    >>typedef struct QUEUENODE *QUEUENODEPTR;
    In this context, QUEUENODEPTR is a typedef for a pointer to a structure of type QUEUENODE. You haven't yet told the compiler what a struct QUEUENODE looks like, but that's OK for now.... until you go like this:
    >>QUEUENODEPTR newPtr;
    >>newPtr->data = value;
    Now you're trying to access a member of a structure that you haven't yet defined, hence the compiler says: dereferencing pointer to incomplete type
    Hum... I got the concept I and fixed the code, it is working now. Thx a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue with templates
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2008, 06:06 AM
  2. Need help with FIFO Queue using Singly Linked Lists
    By astou in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 02:36 PM
  3. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2007, 12:48 AM
  4. queue linked implementation
    By technoXavage in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 04:29 PM
  5. queue question
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2001, 05:06 PM