Home > Net >  compiler converts type character to integer randomly
compiler converts type character to integer randomly

Time:09-15

I'm trying to create a stack with two functions. push (which inserts data at the top of the stack) and pop (which removes data from top of the stack). But at the last line when I want to print the pop item, it gives me the following error!

The Error Message in Terminal

It says "format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’. Segmentation fault (core dumped)."

#include <stdio.h>

#define STACK_SIZE 20

void push(char stack[], int *top, char new){
    if(*top < STACK_SIZE){
    (*top)  ;
    stack[*top] = new;
}
    else
        printf("Stack is full!");
}

char pop(char stack[], int *top){
    char item;
    if(*top >= 0){
        item = stack[*top];
        (*top)--;
    }else{
        printf("stack is empty");
        item = ' ';
    }
    return item;
}

int main(){
    char s[STACK_SIZE];
    int s_top = -1;
    push(s, &s_top, 'C');
    push(s, &s_top, 'A');
    push(s, &s_top, 'H');

    char item = pop(s, &s_top);
    printf("%s", item); // The Error Line?

    return 0;
}

CodePudding user response:

printf("%s", item); // The Error Line?
// Change this %s --> %c
  • Related