【发布时间】:2013-10-16 23:34:58
【问题描述】:
我正在尝试从堆中打印。如果我遇到一个 NULL 指针,我应该打印 NULL;否则,打印它的值。
样本输出:
1 [2]
2 null
3 null
4 [7, 3]
5 null
6 [7]
但是我的代码因为取消引用 NULL 指针而不断崩溃。
这是我写的测试代码:
void printResult(IntList* intL, int nNode, int nEdge)
{
int i;
for (i; i <= 10; i++)
{
if (intRest((intL))
{
printf("%d", intFirst((intL)[i]));
intRest((intL)[i]);
}
else
printf(" NULL ");
}
}
//Here is the definition of functions:
//First
int intFirst(IntList oldL)
{
return oldL->element;
}
/** rest
*/
IntList intRest(IntList oldL)
{
return oldL->next;
}
//=================
struct IntListNode
{
int element;
IntList next;
};
//===================
typedef struct IntListNode * IntList;
【问题讨论】:
-
旁注:
typedef struct IntListNode * IntList;不是个好主意。这很令人困惑。 -
for (i; i <= 10; i++)这是什么? 2)if (intRest((intL))intRest()是什么,为什么需要双括号? -
代码可以编译,但是一旦达到 NULL 就会崩溃。
-
我怀疑它是否可以编译。
标签: c loops pointers null singly-linked-list