viewing paste Unknown #18016 | Text

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <stdio.h>
#define Stack_Max 8
 
int* TopOfStack;
int* CurrentStackPosition;
int Stack[Stack_Max];
 
void PushStack(int);
void PopStack();
 
int main()
{
    TopOfStack = Stack;
    CurrentStackPosition = Stack;
    for(int i = 0; i < Stack_Max; i++)
    {
        PushStack(i);
    }
    for(int i = 0; i < Stack_Max; i++)
    {
        PopStack();
    }
    return 0;
}
 
void PushStack(int pValue)
{
    if(CurrentStackPosition != (TopOfStack + Stack_Max))
    {
        printf("Added %d to the stack at position %d (%d)\n", pValue, CurrentStackPosition, pValue);
        *CurrentStackPosition = pValue;
        ++CurrentStackPosition;
    }
    else    
    {   
        printf("Stack overflow will occur beyond this point. Will not add %d to stack\n", pValue);
    }
}
 
void PopStack()
{
    if(CurrentStackPosition != TopOfStack)
    {
        --CurrentStackPosition;
        printf("%d\n", *CurrentStackPosition);      
    }
    else
    {
        printf("Top of stack reached!");
        return;
    }
}
Viewed 584 times, submitted by Guest.