0

I declared a structure like this

#define max_size 100

struct lifo
{
    int top;
    int st[max_size];
};

I made a function like so:

void create(stack *s)
{
    s->top=-1;
}

I declared a pointer in the main() function:

struct lifo *s;

I called this function like so:

create(s);

but it shows "segmentation fault" at

s->top=-1

I am not sure what is the error

Bodo
  • 8,153
  • 1
  • 12
  • 29
  • 2
    `struct lifo *s;` defines a pointer, but you haven't allocated any memory and it doesn't point to anything valid. So you can't access its members. Try `struct lifo *s = malloc(sizeof *s);` – Weather Vane Dec 02 '21 at 13:10
  • 1
    Please don't show a bunch of code snippets but a [mre], ideally as one code block that can be compiled and run to reproduce the problem. If necessary add the input you use, the output/behavior you get and the expected output/behavior. – Bodo Dec 02 '21 at 13:46
  • @WeatherVane thanks for help – Anirudh Dayanand Dec 02 '21 at 16:22

0 Answers0