C语言Segmentation fault (core dumped)问题排查及两种typedef数组定义方式的区别解析
Hey there! Let's break down your problem into two clear parts: fixing that tricky segmentation fault first, then sorting out the difference between those two typedef array definitions.
一、Segmentation Fault 原因分析与修复
First, let's unpack your typedef line:
typedef struct { char name[30]; int age; } rec[2];
What this actually does is define rec as an array type—specifically, an array that holds 2 instances of your struct. When you declare rec g;, g is exactly this array: it's equivalent to writing struct {char name[30]; int age;} g[2]; directly.
Now, when you write rec *pt = &g;, pt becomes a pointer to this entire array type. Here's where the crash happens: in your loop, you use pt[i]->name and pt[i]->age.
Let's break down pt[i] here: since pt points to an array of 2 structs, pt[i] will jump i * sizeof(rec) bytes in memory. For i=0, this lands on g itself (which is okay), but when i=1, pt[1] points to unallocated memory right after your g array. Accessing this memory triggers the segmentation fault.
On top of that, even for i=0, using -> on pt[0] is incorrect—pt[0] is the array g, not a pointer to a struct.
修复方案
You have two straightforward fixes:
- 直接访问数组元素,跳过指针的复杂用法:
for (int i = 0; i < 2; i++) { printf("Enter name: "); scanf("%s", g[i].name); // 直接用g[i]访问第i个结构体 printf("Enter age: "); scanf("%d", &g[i].age); } - 修改指针类型为结构体指针,让pt指向数组的第一个元素:
// 先单独typedef结构体类型,再定义数组和指针 typedef struct {char name[30]; int age;} Rec; Rec g[2]; Rec *pt = g; // g会隐式转换为指向第一个结构体的指针 // 现在循环可以安全使用 for (int i = 0; i < 2; i++) { printf("Enter name: "); scanf("%s", pt[i].name); printf("Enter age: "); scanf("%d", &pt[i].age); }
二、两种typedef数组定义方式的区别
Let's compare the two snippets side by side to see their core differences:
方式1:将数组类型作为typedef的目标
typedef struct { int a; } example[3]; example v;
- Here,
exampleis an array type: it represents an array that holds exactly 3 instances of the struct. vis a variable of this array type—sovis directly a 3-element array of the struct, and you can't useexampleto declare an array of any other length (it would cause a type mismatch).- If you take a pointer to
vlikeexample *p = &v;,ppoints to the entire 3-element array. This meansp[1]would jump past the entirevarray to unknown memory, not access the second element ofv.
方式2:先typedef结构体,再定义数组变量
typedef struct { int a; } example; example v[3];
- Here,
exampleis a standalone struct type, not an array type. vis an array of 3examplestructs—this is the more common, intuitive way to declare arrays of structs.- If you take a pointer to
vlikeexample *p = v;,ppoints to the first element of the array. Sop[i]correctly accesses the i-th struct in the array, which is what you'd expect in most cases.
核心区别
While both v variables have identical memory layouts (3 structs in a row), the typedef changes how you interact with the type:
- The first way locks the array length into the type itself, which can be restrictive.
- The second way keeps the struct type flexible—you can use
exampleto declare single struct variables, or arrays of any length, making it more versatile for most use cases.
备注:内容来源于stack exchange,提问作者Hello

