我怎樣才能訪問一個結構指針雙指針? 與代碼波紋管,調用addBow()給我一個分段故障(核心轉儲)錯誤雙指針結構內結構
typedef struct
{
int size;
tCity **cities;
}tGraph;
//para iniciar el grafo
void initGraph(tGraph *graph, int size)
{
graph = (tGraph*)malloc(sizeof(tGraph));
graph->cities = (tCity**)malloc(sizeof(tCity*) * size);
graph->size = size;
}
//agrega un arco entre ciudades
void addBow(tGraph *graph, int id, tCity *city)
{
if (graph->cities[id] == NULL)
{
graph->cities[id] = city;
}
else
{
tCity *cur = graph->cities[id];
while (getNext(cur) != NULL)
{
cur = getNext(cur);
}
setNext(cur, city);
}
}
其是用於graph->城市[ID]的正確語法??
感謝
SOLUTION: 編輯initGraph解決問題,因爲內存沒有被分配
tGraph* initGraph(int size)
{
tGraph *graph = (tGraph*)malloc(sizeof(tGraph));
graph->cities = (tCity**)malloc(sizeof(tCity*) * size);
graph->size = size;
return graph;
}
他在那裏存儲指針。 – Inspired
@靈感。同意。更新了答案 –
@ManojPandey Thaanks,解決它,更新。 – JoseMiguel