stack and pointer problem

This is a discussion on stack and pointer problem within the C Programming forums, part of the General Programming Boards category; hello everyone I just beginning my data structure course ad currently i have a problem with stack, I hope you ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    stack and pointer problem

    hello everyone

    I just beginning my data structure course ad currently i have a problem with stack, I hope you guys can help me.

    Ok,this is my code

    Code:
    /* File : stackeng.h */
    /* Type declaration and primitive function prototype for stack type */
    
    #ifndef stack_H
    #define stack_H
    #include "boolean.h"
    #define Nil 0
    #define Top(S) (S).Top
    #define InfoTop(S) (S).T[(S).Top]
    #define Size(S) (S).Size
    
    typedef int infotype;
    typedef int address;
    
    typedef struct
    {
    	infotype * T; /*Table for storing stack element*/
    	address Top; /*(Table index for he top element)*/
    	int Size; /*stack's size*/
    }stack;
    
    /*primitive  prototype  */
    
    void CreateEmpty(stack * S, int size);
    /* Create a  empty stack S whose capacity is as big as size,
    The Index is between 1 until size + 1,because i don't use the 0 index, 
    the stack is empty when the stack's Top property has 0 value  */
    
    void Push(stack * S, infotype x);
    /* add x as elemen stack s
        Initial state :stack S can be empty,the array container for stack elements is not full
        Final state : x become the new top value in stackS,Stack's S Top atribute is incremented by 1  
     */
    
    void Pop(stack * S, infotype * x);
    /* Delete x as stack S element
        Initial state :stack S can not be empty
        Final state : x is a former stack S Top element which had been popped by    Pop  function  
     */
    
    boolean IsEmpty(stack S);
    /* check if stack S is empty ,return true if stack S is empty,false is not empty*/
    
    boolean IsFull(stack S);
    /* check if stack S is full ,return true if stack S is full,false is not full */
    
    void Destruct(stack * S);
    /*Destroy the stack S */
    
    void Grow(stack * S);
    /*Makes the stack capacity grows for one additional element
      Initial state :stack S maybe full,maybe not.The stack's Size property is A
      Final state : The stack's Size property is (A+1)
    */
    #endif
    Code:
    /*Stack's Body primitive function */
    
    #include "stackeng.h"
    #include "boolean.h"
    
    /*BODY PROTOTYPE*/
    
    void CreateEmpty(stack * S, int size)
    /* Create a  empty stack S whose capacity is as big as size,
    The Index is between 1 until size + 1,because i don't use the 0 index, 
    the stack is empty when the stack's Top property has 0 value  */
    {
    	(*S).T = (infotype *) malloc ((size + 1) * sizeof(infotype));
    	Top(*S) = Nil;
    	Size(*S) = size;
    	
    }
    
    void Push(stack * S, infotype x)
    /* add x as elemen stack s
        Initial state :stack S can be empty,the array container for stack elements is not full
        Final state : x become the new top value in stackS,Stack's S Top atribute is incremented by 1  
     */
    {
    	Top(*S)++;
    	InfoTop(*S) = x;
    }
    
    void Pop(stack * S, infotype * x)
    /* Delete x as stack S element
        Initial state :stack S can not be empty
        Final state : x is a former stack S Top element which had been popped by Pop function  
     */
    {
    	*x = InfoTop(*S);
    	Top(*S)--;
    }
    
    boolean IsEmpty(stack S)
    /* check if stack S is empty ,return true if stack S is empty,false is not empty*/
    {
    	return Top(S) == Nil;
    }
    boolean IsFull(stack S)
    /* check if stack S is full ,return true if stack S is full,false is not full */
    {
    	return Top(S) == S.Size;
    }
    
    void Destruct(stack * S)
    /*Destroy the stack S */
    {
    	free((*S).T);
    	Top(*S) = Nil;
    	Size(*S) = 0;
    }
    
    void Grow(stack * S)
    /*Makes the stack capacity grows for one additional element
      Initial state :stack S maybe full,maybe not.The stack's Size property is A
      Final state : The stack's Size property is (A+1)
    */
    {
    	stack S2; /*(temporary stack initialisation)*/
    	CreateEmpty(&S2,Size(*S)); /*(make stack S2 empty )*/
    	S2 = (*S);/*(copy stack S to stack S2)*/
                    Destruct(&S);/*Destroy the stack S */
    	CreateEmpty(&S,(Size(*S2) + 1));/*(Create the new stack S whose    size is one element bigger than the old one)*/
    	S = &S2;/*(copy stack S2 to stack S)*/
    }
    Code:
    /* File: mstackeng.c*/
    
    /*This file is writeen to check whether the primitive function can be implemented in main program or not */
    
    #include <stdio.h>
    #include "boolean.h"
    #include "stackeng.c"
    
    main ()
    {
    	stack S1;
    	CreateEmpty(&S1,9);
    	if(IsEmpty(S1))
    	{
    		printf ("push works well\n");
    		Push(&S1,5);
    	}
    	int x;
    	Pop(&S1,&x);
    	printf ("pop works well %d\n",x);
    	Push(&S1,5);
    	Push(&S1,5);
    	Push(&S1,5);
    	Grow(&S1);
    	int z;
    	scanf ("%d",&z);
        return 0;
    }
    When I compiled file mstackeng.c with Bloodshed Dev C++,i got this following error messages:

    1.line 7 E:\stack\mstackeng.c In file included from E:\stack\mstackeng.c
    E:\stack\stackeng.c In function `Grow':
    2.line 67 E:\stack\stackeng.c [Warning] passing arg 1 of `Destruct' from incompatible pointer type
    3.line 68 E:\stack\stackeng.c invalid type argument of `unary *'
    4.line 68 E:\stack\stackeng.c [Warning] passing arg 1 of `CreateEmpty' from incompatible pointer type

    I tried best to debug my program but it still won' budge.I think the misuse of pointer is the cause of this problem,but i still don't understand how to fix it.

    Can you help me guys?

    Thank you

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Your error comes from passing a stack ** to CreateEmpty. (You're taking the address of a pointer, &S.) Same goes for the Destruct error. Match your types up.

    Also:
    Code:
    S = &S2;/*(copy stack S2 to stack S)*/
    That doesn't do what the comment alleges it does. That set S (a pointer) to the address of S2. S goes out of scope afterwards, and that statement has no effect.

    Also, "Nil" ? The general rule for #defines is to make them all caps. I'd also use NULL over "Nil" - other programs will read "Nil" and go hunt to find out what Nil is. (Since it's not in caps, it's not obvious it's a define, though one might assume things based on context.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Still don't know how to solve it

    Thanks cactus_hugger for your reply.

    But i still don't know how to fix the problem. I mean i still dont know how to pass argument to function CreateEmpty and Destruct correctly in this case.

    Could you write the correct code?,so i can understand how to fix it.

    Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 02:05 AM
  2. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  5. more pointer problems
    By dharh in forum C Programming
    Replies: 3
    Last Post: 02-11-2003, 05:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21