Thread: cannot understand sense of a warning

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    cannot understand sense of a warning

    hello everybody!
    I wrote simple functions to manage a stack, which I had already used and which I saw are very similar (let's say identical) to those shown in Deitel's book:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct stackNode{
    	char data;
    	struct StackNode *nextPtr;
    };
    typedef struct stackNode StackNode;
    typedef StackNode *StackNodePtr;
    
    void push (StackNodePtr *topPtr, char value);
    char pop (StackNodePtr *topPtr);
    int isEmpty(StackNodePtr topPtr);
    void printStack (StackNodePtr topPtr);
    
    int main()
    {
    	srand(time(NULL));
    	int i;
    	StackNodePtr topPtr=NULL;
    	for (i=0;i<10;i++)
    	{
    		push (&topPtr,'a'+rand()%26);
    		printStack(topPtr);
    	}
    	for (i=0;i<10;i++)
    	{
    		pop (&topPtr);
    		printStack(topPtr);
    	}
    }
    
    void push (StackNodePtr *topPtr,char value)
    {
    	StackNodePtr newPtr = (StackNodePtr)malloc(sizeof(StackNode));
    	if (newPtr!=NULL)
    	{
    		newPtr->data=value;
    		newPtr->nextPtr=*topPtr;
    		*topPtr=newPtr;
    		
    	}
    	else
    	{
    		printf("Program run out of memory\n");
    	}
    }
    
    char pop (StackNodePtr *topPtr)
    {
    	if (*topPtr==NULL)
    	{
    		return 0;
    	}
    	else
    	{
    		StackNodePtr tmpPtr=*topPtr;
    		char ret = tmpPtr->data;
    		*topPtr=tmpPtr->nextPtr;
    		free(tmpPtr);
    		return ret;
    	}
    }
    
    int isEmpty(StackNodePtr topPtr)
    {
    	return topPtr==NULL;
    }
    
    void printStack (StackNodePtr topPtr)
    {
    	if (topPtr==NULL)
    	{
    		printf("Stack is empty\n");
    	}
    	else
    	{
    		while (topPtr!=NULL)
    		{
    			printf(" %c -->",topPtr->data);
    			topPtr=topPtr->nextPtr;
    		}
    		printf(" NULL\n\n");
    	}
    }
    after compiling i got this warning:
    Code:
    gcc e12_12.c -o e12_12
    e12_12.c: In function ‘push’:
    e12_12.c:43: warning: assignment from incompatible pointer type
    e12_12.c: In function ‘pop’:
    e12_12.c:63: warning: assignment from incompatible pointer type
    e12_12.c: In function ‘printStack’:
    e12_12.c:100: warning: assignment from incompatible pointer type
    it doesn't like things like:

    Code:
    newPtr->nextPtr=*topPtr;
    which also Deitel uses... Could anybody please explain me this?
    Thanks a lot! bye

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    struct stackNode{
    	char data;
    	struct StackNode *nextPtr;
    };
    There was no resolution for that type at the point of declaration.
    You should notice that the struct is called stackNode, and case matters.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm amazed that the compiler will compile that.
    Anyway, I will suggest that you don't do typedefs like this. The code is extremely confusing to read due to that damn typedef.
    Remove the
    Code:
    typedef StackNode *StackNodePtr;
    And use StackNode* and StackNode** where appropriate. The code will become so much easier to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by xuftugulus View Post
    Code:
    struct stackNode{
    	char data;
    	struct StackNode *nextPtr;
    };
    There was no resolution for that type at the point of declaration.
    You should notice that the struct is called stackNode, and case matters.
    i was sure to get trapped by some s... thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM