我写了一个快速程序,在发出seg故障错误之前执行每个语句。执行完所有程序后在程序结束时发生seg故障?
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;
}
赋予的
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)
结果我真的不知道为什么它赛格末故障?任何意见/见解深表谢意。
,如果你不介意的话,你能告诉我有关的其他问题(尤指一个约void *的参数,因为我碰巧使用非常频繁)。非常感谢! – 2010-04-08 06:00:15
所以它会是 struct foo * food = malloc(sizeof(struct foo)); food =(struct foo *)arg; 在酒吧功能? 因为我仍然陷入seg故障。 – 2010-04-08 06:11:47
哦等待愚蠢的愚蠢它的作品。非常感谢!! – 2010-04-08 06:15:06