Thread: A Quick Question about Stacks

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    A Quick Question about Stacks

    Hi all. I'm new to this place, but I am really stuck and I figure maybe this is the place to go. The following peice of code is EXACTLY what my professor gave the class, and is the only demonstration of stacks he gave us. But, when you try to push items onto the stack, I get an error message! I don't even think the single bit of code he gave us is correct! when i compile (we have to compile under gcc) i get the following error message:

    incompatible type for argument 1 of `push'

    This is my code:

    Code:
    #include <stdio.h>
    
    #define MAXSTACK 20
    
         struct arraystack {
    	int items[MAXSTACK];
    	char quality[MAXSTACK];
    	int top;
    };
    void push( struct arraystack *produce, int x, char q );
    void  pop( struct arraystack *produce ,int *xx, char *qq);
    
    int main( )
    {
    
    struct arraystack factory ;	
    
    factory.top = -1;
    
    push( factory, 4, 'd');  //This is where the error comes in
    return 0;
    }
    
    void push( struct arraystack *produce, int x, char q )
    {
    	if ( produce->top >= MAXSTACK - 1 )
    	  {
    	     printf( "\n% Stack is full.\n");
    	  } 
            else {
    		produce -> top = produce -> top + 1;
    		produce -> items[ produce -> top ] = x;
    		produce -> quality[ produce -> top ] = q;
    	     }
    }
    
    void  pop( struct arraystack *produce ,int *xx, char *qq)
    {
    	int x = 0;
    	if ( produce->top < 0 )
    	{
    	printf("\nStack is empty");
    	}
    	 else {
    		*xx = produce -> items[ produce ->top ];
    		*qq = produce -> quality[ produce ->top ];
    		produce -> top = produce -> top - 1;
    	      }
    }
    what gives????

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    looks like you need to pass the address of your factory variable, it wants a pointer.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    what's "factory" ? Try using arraystack.top instead of factory.top..

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    you aren't initializing factory correctly. Try,

    arraystack factory = new arraystack;
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by durban
    you aren't initializing factory correctly. Try,

    arraystack factory = new arraystack;
    The first rule of C programming is, there is no 'new' in C programming.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Quote Originally Posted by durban
    you aren't initializing factory correctly. Try,

    arraystack factory = new arraystack;
    lool Dude, you are on hardcore programming grounds. There are no objects like in higher languages here

  7. #7
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by rockytriton
    looks like you need to pass the address of your factory variable, it wants a pointer.

    Indeed. And you need & to pass the address.

    Code:
    push( &factory, 4, 'd');

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    ouch..havent seen the code properly before. I didn't see that factory is a pointer to your struct. Yes. Like the man before me said, pass the address of the variable. See how push and pop are defined. THey both expect a pointer as the first argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM