-
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??
-
Actually now Im getting the letter O...
-
>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.