【发布时间】:2009-11-23 15:27:16
【问题描述】:
我是 C 初学者,我很好奇为什么每次都会出现 Seg Fault:
#include <stdio.h>
#include <stdlib.h>
struct Wrapper {
int value;
};
int main () {
struct Wrapper *test;
test->value = 5;
return 0;
}
我知道我还没有完全理解指针,但我认为
struct_ptr->field
和
一样(*struct_ptr).field
所以尝试对该字段进行分配权限应该没问题。这就像预期的那样工作:
struct Wrapper test;
test.value = 5;
但我很好奇为什么使用指针会导致 Seg Fault。
我使用的是 Ubuntu 9.04 (i486-linux-gnu),gcc 版本 4.4.1
【问题讨论】: