You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言Segmentation fault (core dumped)问题排查及两种typedef数组定义方式的区别解析

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:

  1. 直接访问数组元素,跳过指针的复杂用法:
    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);
    }
    
  2. 修改指针类型为结构体指针,让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, example is an array type: it represents an array that holds exactly 3 instances of the struct.
  • v is a variable of this array type—so v is directly a 3-element array of the struct, and you can't use example to declare an array of any other length (it would cause a type mismatch).
  • If you take a pointer to v like example *p = &v;, p points to the entire 3-element array. This means p[1] would jump past the entire v array to unknown memory, not access the second element of v.

方式2:先typedef结构体,再定义数组变量

typedef struct {
    int a;
} example;
example v[3];
  • Here, example is a standalone struct type, not an array type.
  • v is an array of 3 example structs—this is the more common, intuitive way to declare arrays of structs.
  • If you take a pointer to v like example *p = v;, p points to the first element of the array. So p[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 example to declare single struct variables, or arrays of any length, making it more versatile for most use cases.

备注:内容来源于stack exchange,提问作者Hello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 07:58:07