seg fault at the end of program after executing everything?

Posted by Fantastic Fourier on Stack Overflow See other posts from Stack Overflow or by Fantastic Fourier
Published on 2010-04-08T05:38:21Z Indexed on 2010/04/08 5:43 UTC
Read the original article Hit count: 283

Filed under:
|

Hello all, I wrote a quick program which executes every statement before giving a seg fault error.

struct foo
{
    int cat;
    int * dog;
};

void bar (void * arg)
{
    printf("o hello bar\n");
    struct foo * food = (struct foo *) arg;
    printf("cat meows %i\n", food->cat);
    printf("dog barks %i\n", *(food->dog));
}
void main()
{
    int cat = 4;
    int * dog;
    dog = &cat;

    printf("cat meows %i\n", cat);
    printf("dog barks %i\n", *dog);

    struct foo * food;
    food->cat = cat;
    food->dog = dog;

    printf("cat meows %i\n", food->cat);
    printf("dog barks %i\n", *(food->dog));

    printf("time for foo!\n");
    bar(food);
    printf("begone!\n");

    cat = 5;
    printf("cat meows %i\n", cat);
    printf("dog barks %i\n", *dog);

//  return 0;
}

which gives a result of

cat meows 4
dog barks 4
cat meows 4
dog barks 4
time for foo!
o hello bar
cat meows 4
dog barks 4
begone!
cat meows 5
dog barks 5
Segmentation fault (core dumped)

I'm not really sure why it seg faults at the end? Any comments/insights are deeply appreciated.

© Stack Overflow or respective owner

Related posts about segmentation-fault

Related posts about c