Thread: Weird stack problem...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    Weird stack problem...

    OK, Im not sure why this is happening, but Ive traced my code all the way back to the top and this is just the beginning of my program...

    Code:
       typedef struct NodeT
       {
          struct NodeT *L, *R;  
          char info;
       } NodeT;
    	
       typedef struct
       {
          NodeT * array[80];  
          int tos;
       } StackT;
    	
       typedef struct
       {
          char iArray[81];  
          int current;
       } Input;
    	
    	
       NodeT getCharacter(Input *);
       NodeT peekCharacter(Input *);
       void pushStack(StackT *, NodeT *);
       NodeT * popStack(StackT *);
       NodeT * combine(NodeT *, NodeT *, NodeT *);
       NodeT * peekStack(StackT *);
       void initStack(StackT *);
       void initInput(Input *);
       NodeT newNode(char);
       void printPrefix(NodeT *);
       void printInfix(NodeT *);
       void printPostfix(NodeT *);
       void displayStack(StackT *);
       int getAction(char, NodeT *, int[][5]);
       int getRow(char);
       int getCol(NodeT *);
    
        int main()
       {
          Input iList;
       	
          StackT OperatorStack;
          StackT OperandStack;
       
          initStack(&OperatorStack);
          initStack(&OperandStack);
          initInput(&iList);
       	
          int table[8][5] = {1,1,1,1,5,
          	           	          4,4,2,2,5,
          		          2,4,2,2,5,
          		          2,2,2,2,5,
          	                      4,4,3,5,5,
          		          6,6,6,6,5,
          		          7,7,7,7,5,
          		          5,5,5,5,5};
    
          fprintf(stderr, "%c", OperatorStack.array[OperatorStack.tos]);
    
          return 0;
       	
       }
    
    
        void initStack(StackT *stack)
       {
          stack->tos = 0;
       }
    
    
        void initInput(Input *i)
       {
          i->current = 0;
       	
          fprintf(stderr, "Please enter an infix expression: ");
          scanf("%s", i->iArray);
       }
    Basically, this is the beginnings of my program, although I have done a lot more; this seems to be where the trouble is coming from...

    The final printf statement before the end of main prints out the letter S...

    I have no idea why...

    Ha, I never set it to anything??

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Actually now Im getting the letter O...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have no idea why...
    You're printing whatever resides in that particular piece of memory at that time. Technically you shouldn't be trying to print until you've properly initialized the object, because even looking at an uninitialized variable invokes undefined behavior.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Stringy Sums
    By bumfluff in forum C++ Programming
    Replies: 14
    Last Post: 05-15-2006, 01:52 AM
  4. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  5. weird problem with passing a value..
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 06-21-2002, 09:05 AM