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

关于对不完整类型struct Graph_Edge非法使用sizeof的报错咨询

Fixing the "invalid application of ‘sizeof’ to incomplete type" Error with Mutually Referencing Structs

Hey there! Let's figure out why this error is popping up and how to fix it.

First, let's break down the root cause: When you have two structs that reference each other (like Graph_Node and Graph_Edge), C needs a little heads-up that each struct exists before you use it—this is called a forward declaration. But here's the critical part: when you use sizeof(struct Graph_Edge) in your malloc call, the compiler needs to know the full size of that struct. If it only has a forward declaration (and not the complete struct definition), it can't calculate the size—hence the "incomplete type" error.

Here's what's happening in your code

If your code is ordered like this (out of sequence), you'll hit the error:

// You define Graph_Node first, which references Graph_Edge
typedef struct Graph_Node {
    struct Graph_Edge* edges; // Compiler knows Graph_Edge exists, but not its size
} graph_node;

// Then you try to malloc an Edge before defining it
graph_edge* insert_edge() {
    // Error hits here: compiler hasn't seen the full Graph_Edge definition yet
    graph_edge* new_edge = malloc(sizeof(struct Graph_Edge));
    return new_edge;
}

// Finally, you define Graph_Edge
typedef struct Graph_Edge {
    graph_node* from;
    graph_node* to;
} graph_edge;

The insert_edge function runs before the compiler sees the complete Graph_Edge struct. Since sizeof needs to know all the members of the struct to compute its size, it throws an error.

The Fix: Forward Declare + Order Your Definitions Correctly

Here's the step-by-step fix to resolve this:

  1. Forward declare both structs first — tell the compiler these structs exist, even if you haven't defined them yet.
  2. Define one struct (it can use pointers to the other, since forward declarations work for pointers).
  3. Define the second struct (now it can reference the first struct, which is already fully defined).
  4. Make sure any code using sizeof comes after the full struct definition.

Here's the corrected code example:

// Step 1: Forward declarations
struct Graph_Node;
struct Graph_Edge;

// Step 2: Define Graph_Node (uses a pointer to Graph_Edge, which is forward-declared)
typedef struct Graph_Node {
    int node_id;
    struct Graph_Edge* adjacent_edges;
} graph_node;

// Step 3: Define Graph_Edge (can now safely reference graph_node, since it's fully defined)
typedef struct Graph_Edge {
    graph_node* from_node;
    graph_node* to_node;
    int weight;
    struct Graph_Edge* next_edge;
} graph_edge;

// Step 4: Now insert_edge can use sizeof(graph_edge) safely
graph_edge* insert_edge(graph_node* from, graph_node* to, int weight) {
    graph_edge* new_edge = malloc(sizeof(graph_edge)); // No more error!
    if (new_edge == NULL) {
        // Handle malloc failure
        return NULL;
    }
    new_edge->from_node = from;
    new_edge->to_node = to;
    new_edge->weight = weight;
    new_edge->next_edge = NULL;
    return new_edge;
}

Why this works

  • Forward declarations let the compiler recognize that struct Graph_Edge and struct Graph_Node are valid types (even before their full definitions), so you can use pointers to them in other structs without issue.
  • By defining both structs before writing the insert_edge function, the compiler has the full struct definition when it hits the sizeof call—so it can calculate the exact memory needed for the edge.

内容的提问来源于stack exchange,提问作者user8955046

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:53:15