{"record":{"id":"c30b230d9751d348","repo":"krahets/hello-algo","slug":"error-c30b23","errorCode":null,"errorMessage":"邊索引越界或相等\n","messagePattern":"邊索引越界或相等\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"zh-hant/codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":80,"sourceCode":"    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // 在鄰接矩陣中刪除索引 index 的列\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }\n    graph->size--;\n}\n\n/* 新增邊 */\n// 參數 i, j 對應 vertices 元素索引\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"邊索引越界或相等\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* 刪除邊 */\n// 參數 i, j 對應 vertices 元素索引\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"邊索引越界或相等\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* 列印鄰接矩陣 */","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/c/chapter_graph/graph_adjacency_matrix.c#L62-L98","documentation":"Traditional-Chinese localization of the addEdge guard (see error 602). addEdge forbids negative/out-of-range endpoints and self-loops (i == j) because this is a simple undirected graph. Logged to stderr; adjMat unchanged.","triggerScenarios":"addEdge(graph, i, j) with i/j out of range or equal — typically vertex values mistaken for indices, or a self-loop.","commonSituations":"Value-vs-index confusion; self-loops; edges created before endpoints are inserted.","solutions":["Pass indices, not values.","Insert both vertices first.","Forbid i == j at the call site."],"exampleFix":"// before\naddEdge(graph, 1, 3);\n\n// after\nint i = indexOf(graph, 1), j = indexOf(graph, 3);\nif (i >= 0 && j >= 0 && i != j) addEdge(graph, i, j);","handlingStrategy":"validation","validationCode":"static inline int graphValidEdge(const GraphAdjMat *g, int i, int j) {\n    return g != NULL && i >= 0 && j >= 0\n        && i < g->size && j < g->size && i != j;\n}\n\nif (graphValidEdge(graph, i, j)) addEdge(graph, i, j);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass indices, not values; name variables to make this clear.","Insert both endpoints before adding the edge.","Reject i == j at the call site for this simple-undirected model."],"tags":["graph","bounds","edge","self-loop","adjacency-matrix","c","i18n-zh-hant"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}