在一個本地函數中,我想計算圖中有多少個節點指向彼此(in-degree),並且它由相鄰列表表示。奇怪的是,當我在這個函數中打印時,我所有的數字都是正確的,但是當我離開這個函數時,我失去了其中的一半。C - 指向int的本地指針丟失了一半大小
VertexType *InDegree(Graph G)
{
int Size, i;
Size = G->GraphSize; // Size is 17
VertexType *degree;
for (i = 0; i < Size; ++i)
degree[ i ] = 0;
for (i = 0; i < Size; ++i)
{
VertexNodePtr P;
P = G->Vertices[ i ].AdjList;
// A traverse, not relevant to this question
while (P != NULL)
{
++degree[ P->Vertex ];
P = P->Next;
}
}
// here I can print all the numbers using a
// for-loop counting to 17, correctly.
// but sizeof() gives me half the length I expect!
// which is 8. And out of this function counting to
// 17 causes an overflow.
return degree;
}
謝謝,我檢查了它忽略'sizeof',這是正確的。溢出必須是其他地方的錯誤。 – knh170 2014-12-03 15:49:02
'sizeof'是一個運算符,而不是宏。 – Clifford 2014-12-03 15:49:39