Thread: Stack

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    29

    Stack

    Hi all, I'm fairly new to C programming and am currently trying to create a stack just using pointers.

    I have created a function 'peek' that will print out all of the integers that are stored in the stack.

    I want to pass the stack by value, so no changes are made to it outside of the function.

    This doesn't work, and the program just crashes...as I'm using pointers, I'm guessing that I am missing something out to do with memory.

    The code does work, however, when I pass the stack by reference...

    Code:
     peek(stack,sizeof(stack));
    Code:
     
    void peek(stack_t **stackptr, int size)
    {
    	stack_t* peek_element;
    	int i,data;
    	
    	for ( i=0; i < size; i++)
    	{
    		peek_element = (*stackptr);       				
          	        data = peek_element->data; 	    				
    		(*stackptr) = peek_element->next;
    		printf("%d ", data);
    	}
    	
    }
    If anyone could either tell me what I am doing wrong, or point me in the right direction it would be greatly appreciated.

    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    peek(stack,sizeof(stack));
    You'd probably want to pass the number of elements currently in the stack instead.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Ok, I tried that by incrementing 'size_stack' each time 'push' is called.

    Then passing that value instead of sizeof(stack);

    Had no effect

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here's an idea: Why don't you post a complete snippet of code that demonstrates the problem? Otherwise you are asking us to debug where you think the problem is rather than actually debugging your code. Debugging code is relatively easy compared to debugging other people's thoughts.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Sorry...didn't want to overwhelm you with all of my code.

    Here it is anyway:

    Stack.h (Using this so I get used to header files etc...)

    Code:
    typedef struct stack{
    int data;
    struct stack* next;
    }
    stack_t;
    
    void push(int, stack_t** stackptr);
    int pop(stack_t** stackptr);
    void peek(stack_t** stackptr, int);

    Stack.C
    Code:
    #include<stdio.h>
    #include"stack.h"
    
    stack_t** stack = NULL;  //Global variable as both functions use it.
    int stack_size = 0;
    int main()
    {
    	int a,b,c;
    
    	printf("Enter any number on the keyboard ");
    	scanf("%d", &a);					 
    				 		
    	printf("Enter any number on the keyboard ");
    	scanf("%d", &b); 							
    		
    	printf("Enter any number on the keyboard ");
    	scanf("%d", &c); 							
    
    	push(a, &stack);
    	push(b, &stack);
    	push(c, &stack);
    	
    	peek(stack, stack_size);
    
    	printf("%d ", pop(&stack));
    	printf("%d ", pop(&stack));
    	printf("%d ", pop(&stack));
    	
     return 0;
    }
    
    void push(int data, stack_t **stackptr)   
    {
    	stack_t* push_element;
                    
    	push_element = (stack_t*)  malloc(sizeof(stack_t));	//Allocating memory 		
    	push_element->data = data; 					//Data assigned
    	push_element->next = (*stackptr);				//Next is now equal to the address  [which is of pointer type]  that stackptr  [pointer to a pointer]  points to.  
    	(*stackptr) = push_element;   				//Address stackptr points to now stores new_element
    	stack_size++;
    }
    
    int pop(stack_t **stackptr)
    {
    	int data;
    	stack_t* pop_element;
    
    	pop_element = (*stackptr);       				//Old_element is now equal to the address  [which is of pointer type]  that stackptr  [pointer to a pointer]  points to.  	
          data = pop_element->data; 	    				//Data is copied 
    	(*stackptr) = pop_element->next;  				//Value pointed to by stackptr is = old_element->next.              
    	free(pop_element); 		     				//Remove pointer after usage.
    	return data;
    	stack_size--;
    }
    
    void peek(stack_t **stackptr, int size)
    {
    	stack_t* peek_element;
    	int i,data;
    	
    	for ( i=0; i < size; i++)
    	{
    		peek_element = (*stackptr);       				//Old_element is now equal to the address  [which is of pointer type]  that stackptr  [pointer to a pointer]  points to.  	
          	data = peek_element->data; 	    				//Data is copied 
    		(*stackptr) = peek_element->next;
    		printf("%d ", data);
    	}
    	
    }
    Last edited by zonf; 12-12-2005 at 11:34 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need a pointer to a pointer for your stack. You only need that for your function calls, so they can update the passed stack. You really don't need any arguments to your functions, though it's a good idea, since you're using a global anyway...

    Also, because 'stack' is a pointer to a pointer, doing '&stack' is in effect a pointer to a pointer to a pointer. All you need to have is a single pointer:
    Code:
    foo_t *stack;
    ...
    push( &stack, bar );
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Sorry, I am extremely confused...I've spent most of the day doing this thing, and I hate pointers...

    I no longer need to use
    Code:
    stack_t **stack
    but use

    Code:
    stack_t *stack
    So now that is simply a pointer of type 'stack_t'.

    Is this all that is needed to be changed? I have done so, and the program compiles far better now (less errors :$) but the same problem occurs....

    Program crashes after Peek has been called.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    No its ok, sorry, works fine now, my mistake!!!

    Thanks

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Hi, me again...I'm really sorry to keep asking questions, but I've spent the last hour or so trying to work out why, when I enter in 3 values, when it comes to popping the last value, it will come out as 0.

    Why is that happening? Surely if the 'peek' function displays all the values correctly, and the stackptr is returned to its orginal value before the function, then pop should have no problems?

    I can't think what is wrong with it...

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for one thing, you never check to see if your stack is empty before you go popping it apart. Also, you have a return statement before you decrement the stack size, so that line of code never happens. When you return, nothing after the return happens.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Hi all, me again. I had my programming working completely correctly, and that was using the GCC windows compiler.

    Now I move onto fedora core, it's giving me alot of weird errors, errors that I can't see....

    here is my complete code, again!

    Code:
    #include<stdio.h>
    
    #include"stack.h"
    
    
    
    stack_t* stack = NULL;  		//Global variable as both functions use it.
    
    int stack_size = 0;		//Size of stack (number of values inside it)
    
    int main(int argv, char*argc[])
    {
    
    	int a,b,c,d;
    
    	printf("Enter any 4 integers seperated by spaces ");
    
    	scanf("%d %d %d %d", &a,&b,&c,&d);			          //Read in user input					
    
    
    
    	push(a, &stack);							    //Push each value onto the stack
    
    	push(b, &stack);
    
    	push(c, &stack);
    
    	push(d, &stack);
    
    	
    
    	peek(&stack,stack_size);					    //Call peek
    
    	printf("::%d:: is popped off the stack\n", pop(&stack)); //Pop a value off the stack
    
    	peek(&stack,stack_size);					    //Call peek
    
    	printf("::%d:: is popped off the stack\n", pop(&stack)); //Pop a value off the stack	
    
    	peek(&stack,stack_size);					    //Call peek
    
     return 0;
    
    }
    
    
    
    /*::::::::::::::FUNCTIONS::::::::::::::*/
    
    
    
    
    
    void push(int data, stack_t **stackptr)   			//Pushes integer onto the stack
    
    {
    
    	stack_t* push_element; 						//Stack pointer
    
                    
    
    	push_element = (stack_t*)  malloc(sizeof(stack_t));	//Allocating memory 		
    
    	push_element->data = data; 					//Data assigned
    
    	push_element->next = (*stackptr);				//Next is now equal to the address  [which is of type pointer ]  that stackptr  [pointer to a pointer]  points to.  
    
    	(*stackptr) = push_element;   				//Address stackptr points to now stores new_element
    
    	stack_size++;							//Increment stack size, as integer has been pushed
    
    }
    
    
    
    int pop(stack_t **stackptr)						//Pop integer off the stack
    
    {
    
    	int data;								//Used to store data
    
    	stack_t* pop_element;						//Stack pointer
    
    	if (stack_size != 0)						//If stack is not empty
    
    	{
    
    		pop_element = (*stackptr);       			//pop_element is now equal to the address  [which is of type pointer ]  that stackptr  [pointer to a pointer]  points to.  	
    
          	data = pop_element->data; 	    			//Data is copied 
    
    		(*stackptr) = pop_element->next;  			//Stackptr now points to pop_element->next.              
    
    		free(pop_element); 		     			//Remove pointer after usage.
    
    		stack_size--;						//Decrement stack size, as integer has been popped.
    
    		return data;						//Return the data that has been popped
    
    	}
    
    }
    
    
    
    void peek(stack_t **stackptr, int size)				//Displays all values in the stack
    
    {
    
    	stack_t* peek_element;						//Stack pointer
    
    	stack_t* store_element;						//Stack pointer	
    
    	int i;	
    	int data;
    	store_element = (*stackptr);					//Stack pointer stores address that *stackptr points to.
    
    	printf("Peeking at stack:: ");			
    
    	for ( i=0; i < size; i++)					//Loop untill i = stack size.
    
    	{
    
    		peek_element = (*stackptr);       			//Peek_element is now equal to the address  [which is of type pointer ]  that stackptr  [pointer to a pointer]  points to.  	
    
          	data = peek_element->data; 	    			//Data is copied 
    
    		(*stackptr) = peek_element->next;			//Stackptr now points to peek_element->next 
    
    		printf("%d ", data);					//Prints value of data
    
    	}
    
    	printf("\n");							//New line
    
    	(*stackptr) = store_element;					//*stackptr now points to store_element, restoring it's orginal value.
    
    }


    The following are the errors I get...

    stack2.c: In function `peek':
    stack2.c:4: error: parameter `stack' is initialized
    stack2.c:5: error: parameter `stack_size' is initialized
    stack2.c:7: error: syntax error before '{' token
    stack2.c:43: error: syntax error before "if"
    stack2.c:60: error: syntax error before "store_element"

    I can't see any errors in what I have done? I have bolded them so you can find them easily.

    I'm really sorry to keep on asking, but I just cannot understand why this isn't working.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My guess is a prototype in your stack.h is missing a ) from looking at the first error.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Sorry, what's a prototype?


    This is my stack.h
    Code:
    typedef struct stack{
    int data;
    struct stack* next;
    }
    stack_t;
    
    void push(int, stack_t** stackptr);
    int pop(stack_t** stackptr);
    void peek(stack_t** stackptr, int);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM