在很多例子中,我读过一个简单的 getListLength() 函数看起来像这样:
int getListLength(struct node *head)
{
struct node *temp = head;
int iCount = 0;
while (temp)
{
++iCount;
temp = temp->next;
}
return iCount;
}
令我印象深刻的是不需要声明复制传递参数的本地指针(在本例中为 *temp)。如果我没记错的话,传递的参数会获得它们自己的副本。因此,将不需要复制 *head 的本地指针,因为 *head 本身就是副本,对吗?换句话说,丢弃 *temp 指针并在任何地方使用 head 是否正确?