如何在C中显示链表中节点的地址
这是一个循环链表,C语言中的插入函数 我需要显示第一个节点的地址和最后一个节点的链接部分具有相同的值,从而显示它是一个循环链表如何在C中显示链表中节点的地址,c,linked-list,C,Linked List,这是一个循环链表,C语言中的插入函数 我需要显示第一个节点的地址和最后一个节点的链接部分具有相同的值,从而显示它是一个循环链表 但我上面的代码给出了随机整数值 您可能希望打印带有%p的地址 printf最后一个节点的地址为&d,nn->next 这是不对的。 尝试使用以下命令打印指针变量的地址 printf("The address of the first node is %p",first); printf("The address of the last node is %
但我上面的代码给出了随机整数值 您可能希望打印带有%p的地址 printf最后一个节点的地址为&d,nn->next 这是不对的。 尝试使用以下命令打印指针变量的地址
printf("The address of the first node is %p",first);
printf("The address of the last node is %p",nn->next);
列出了常用的格式说明符。可移植打印的唯一地址是空指针。这不是一个很大的限制,因为您可以将任何对象指针转换为空指针并返回。ptr要作废的printf说明符是%p:
请注意,void*强制转换是为了避免未定义的行为。这在任何情况下都是未定义的行为。%x不接受指针,只接受整数。是的,它未定义,但只要int和pointer的大小相同%x就应该正确显示地址。printf的As参数作为值传递。所以,如果地址是0x01010101,并作为值传递。只能在printf中读取为4字节的数字。。。如果我错了,请纠正我。你错了。未定义的是未定义的是未定义的。任何猜测都是假的。有一种方法可以以定义的方式打印指针。使用它。在C中,我们不会从malloc中抛出返回。搜索此网站以了解原因。
printf("The address of the first node is %p",first);
printf("The address of the last node is %p",nn->next);
printf("The address of the last node is 0x%p",nn->next);
/*
Below Will Fail To Print Correctly
if sizeof(int) != sizeof(void*)
*/
printf("The address of the last node is 0x%x",nn->next);
printf("The address of the first node is %p\n", (void *)first);
printf("The address of the last node is %p\n", (void *)nn->next);