Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6001369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:49:29+00:00 2026-05-23T00:49:29+00:00

I found some code to make a C implementation of stacks, and decided to

  • 0

I found some code to make a C implementation of stacks, and decided to use it. However, there were several typedefs, and I am having difficulty printing the values in a stackT (really a char array). Below is the code. What am I doing wrong?

#include <stdio.h>
#include <stdlib.h>

typedef char stackElementT;

typedef struct {
  stackElementT *contents;
  int maxSize;
  int top;
} stackT;

void StackInit(stackT *stackP, int maxSize) {
    stackElementT *newContents;
    newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize);
    if (newContents == NULL) {
        fprintf(stderr, "Not enough memory.\n");
        exit(1);
    }

    stackP->contents = newContents;
    stackP->maxSize = maxSize;
    stackP->top = -1; //empty...
}

void StackDestroy(stackT *stackP) {
    free(stackP->contents);
    stackP->contents = NULL;
    stackP->maxSize = 0;
    stackP->top = -1; //empty
}

int StackIsEmpty(stackT *stackP) {
    return stackP->top < 0;
}

int StackIsFull(stackT *stackP) {
    return stackP->top >= stackP->maxSize-1;
}

void StackPush(stackT *stackP, stackElementT element) {
    if(StackIsFull(stackP)) {
        fprintf(stderr, "Can't push element: stack is full.\n");
        exit(1);
    }
    stackP->contents[++stackP->top] = element;
}

stackElementT StackPop(stackT *stackP) {
    if(StackIsEmpty(stackP)) {
        fprintf(stderr, "Can't pop element: stack is empty.\n");
        exit(1);
    }
    return stackP->contents[stackP->top--];
}

void StackDisplay(stackT *stackP) {
    if(StackIsEmpty(stackP)) {
        fprintf(stderr, "Can't display: stack is empty.\n");
        exit(1);
    }
    int i;
    printf("[ ");
    for (i = 0; i < stackP->top; i++) {
        printf("%c, ", stackP[i]); //the problem occurs HERE
    }
    printf("%c ]", stackP[stackP->top]);
}

int postfix(char* expr, int length) {
    int i;
    stackT stack;
    StackInit(&stack, 1000);
    int temp;
    for (i = 0; i < length; i++) {
        if ((expr[i] >= 48) && (expr[i] <= 57)) {
            printf("Is a number! Pushed %d\n", expr[i]);
            StackPush(&stack, expr[i]);
        }
        else {
            switch (expr[i]) {
                case 43: {
                    temp = StackPop(&stack);
                    StackPush(&stack, StackPop(&stack)+temp);
                }
                    break;
                case 45: {
                    temp = StackPop(&stack);
                    StackPush(&stack, StackPop(&stack)-temp);
                }
                    break;
                case 47: {
                    temp = StackPop(&stack);
                    StackPush(&stack, StackPop(&stack)/temp);
                }
                    break;
                case 42: {
                    temp = StackPop(&stack);
                    StackPush(&stack, StackPop(&stack)*temp);
                }
                    break;
                default:
                    break;
            }
        }
    }
    return StackPop(&stack);
}

int main() {
    int i;
    char* expr = "1 2 3 + * 3 2 1 - + *";
    for(i = 0; expr[i] != '\0'; i++) ;
    printf("%d\n", postfix(expr, i));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:49:29+00:00Added an answer on May 23, 2026 at 12:49 am

    The compiler (GCC 4.2.1 on MacOS X 10.6.7) tells me:

    $ cc -O -std=c99 -Wall -Wextra     st.c   -o st
    st.c: In function ‘StackDisplay’:
    st.c:72: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘stackT’
    st.c:74: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘stackT’
    $
    

    In my version of the code, these two lines are the printf() statements in StackDisplay(),
    right where you state you have problems.

    void StackDisplay(stackT *stackP)
    {
        if(StackIsEmpty(stackP)) {
            fprintf(stderr, "Can't display: stack is empty.\n");
            exit(1);
        }
        int i;
        printf("[ ");
        for (i = 0; i < stackP->top; i++) {
            printf("%c, ", stackP[i]); //the problem occurs HERE
        }
        printf("%c ]", stackP[stackP->top]);
    }
    

    You probably want stackP->contents[i]. With that fix, the program ‘runs’ but produces:

    Can't pop element: stack is empty.
    

    That is your problem to resolve, now.


    (Oh, I also fixed the stray semi-colon after the for loop in main() as diagnosed in the comments.)

    The loop should be written as strlen(expr) (and then you need to #include <string.h>). Indeed, the body of the main program simplifies to:

    char* expr = "1 2 3 + * 3 2 1 - + *";
    printf("%d\n", postfix(expr, strlen(expr)));
    

    You should normally keep top indexed to the next location to use, so the initial value would normally be 0 rather than -1.

    Don’t learn the ASCII codes for the digits – forget you ever did.

        if ((expr[i] >= 48) && (expr[i] <= 57)) {
    

    You should write:

        if ((expr[i] >= '0') && (expr[i] <= '9')) {
    

    or, better (but you have to #include <ctype.h> too):

        if (isdigit(expr[i])) {
    

    Similar comments apply to the switch:

            switch (expr[i]) {
                case 43: {
                    temp = StackPop(&stack);
                    StackPush(&stack, StackPop(&stack)+temp);
                }
                    break;
    

    I’m not sure of the logic behind the indentation, but that 43 should be written as '+', 45 as '-', 47 as '/', and 42 as'*'.


    This generates:

    Is a number! Pushed 49
    Is a number! Pushed 50
    Is a number! Pushed 51
    Is a number! Pushed 51
    Is a number! Pushed 50
    Is a number! Pushed 49
    68
    

    If you fix the number pushing code as shown:

    printf("Is a number! Pushed %d\n", expr[i] - '0');
    StackPush(&stack, expr[i] - '0');
    

    Then you get:

    Is a number! Pushed 1
    Is a number! Pushed 2
    Is a number! Pushed 3
    Is a number! Pushed 3
    Is a number! Pushed 2
    Is a number! Pushed 1
    20
    

    And with a little more instrumentation, along the lines of:

    temp = StackPop(&stack);
    printf("Sub: result %d\n", temp);
    StackPush(&stack, temp);
    

    after each operation, the result is:

    Is a number! Pushed 1
    Is a number! Pushed 2
    Is a number! Pushed 3
    Add: result 5
    Mul: result 5
    Is a number! Pushed 3
    Is a number! Pushed 2
    Is a number! Pushed 1
    Sub: result 1
    Add: result 4
    Mul: result 20
    20
    

    You were close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some code in a project which looks like that : int main(int
I have found some code snippet in the internet. But it is missing some
I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
I got a task to maintain a c# project, I found some code like
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I found some mapkit code on the internet that looks like this: - (void)recenterMap
I've just found some C++ code (at http://msdn.microsoft.com/en-us/library/k8336763(VS.71).aspx ), which uses a technique I've
While browsing some code I found a call to OpenPrinter() . The code compiles
This is some code I found on the internet. I'm not sure how it
Taking over some code from my predecessor and I found a query that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.