Click here to Skip to main content
15,615,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
#include <stdio.h>
#include <stdlib.h>

struct stack
{
    int size;
    int top;
    int *arr; 
};
int stack_is_empty(struct stack *s)
{
    //for an empty stack top=-1

    if ((s->top) == -1)
        return 1; // return true
    else
        return 0; //return false
}
int stack_is_full(struct stack *s)
{
    //for a full stack top= size-1;

    if ((s->top) == (s->size - 1))
        return 1; // return true
    else
        return 0; //return false
}
int main(int argc, char const *argv[])
{
    struct stack *s;
    s->size = 80;
    s->top = -1;
    s->arr = (int*)malloc(s->size * sizeof(int)); 
    if (stack_is_empty(s))
        printf("%s", "THE STACK IS EMPTY\n");
    else
        printf("%s", "THE STACK IS NOT EMPTY\n");
    return 0;
}


What I have tried:

I have tried using fflush but still nothing is printed in vs code
Posted
Updated 20-Aug-21 22:43pm

Did you tried this :
C++
int main(int argc, char const *argv[])
{
    struct stack *s;
    struct stack s;


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Because you forgot to allocate space for your stack in main, so every reference to it is addressing some random piece of memory. It should be:
C++
int main(int argc, char const *argv[])
{
    struct stack *s;
    s = (struct stack*)malloc(sizeof(struct stack));
    s->size = 80;
    s->top = -1;
    s->arr = (int*)malloc(s->size * sizeof(int)); 
    if (stack_is_empty(s))
        printf("%s", "THE STACK IS EMPTY\n");
    else
        printf("%s", "THE STACK IS NOT EMPTY\n");
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900