0

Iam trying to run this program for a very long time but i dont know why again and again segmentation fault error is coming (iam running this program on vs code and my OS is LINUX)

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

struct stack
{
int size;
int top;
int *arr;
};

int isEmpty(struct stack *ptr)
{
if (ptr->top == -1)
{
    return 1;
}
else
{
    return 0;
}
}

int isFull(struct stack *ptr)
{
if (ptr->top == ptr->size - 1)
{
    return 1;
}
else
{
    return 0;
}
}

int main()
{

struct stack *s;
s->size = 80;
s->top = -1;
s->arr = (int *)malloc(s->size * sizeof(int));

// Pushing an element manually
s->arr[0] = 7;
s->top++;

// Check if stack is empty
if (isEmpty(s))
{
    printf("The stack is empty");
}
else
{
    printf("The stack is not empty");
}
return 0;
}

please solve this error because iam stuck at this error and i dont know why this error is coming

  • 3
    You haven't allocated any memory to `s`. You need to call `malloc` when doing `struct stack *s;` – costaparas Jan 06 '21 at 12:07
  • 1
    You could remove pointer from `struct stack *s` or you could allocate memory for your struct with `malloc` – Kanony Jan 06 '21 at 12:11

0 Answers0