c - 在C中的链表中打印节点的十六进制内存地址
问题描述
我有一个链表,它接受一个输入字符串并将每个字符串存储在列表的一个节点中。我想打印保存每个字符串的节点的十六进制地址。
我怎样才能做到这一点?我尝试打印保存字的十六进制地址,但我还不知道它是否仍然是节点的相同地址,这是应该打印每个节点的函数:
// print the list
void printList(ListNodePtr currentPtr)
{
// if list is empty
if (isEmpty(currentPtr)) {
puts("List is empty.\n");
}
else {
puts("The list is:");
// while not the end of the list
while (currentPtr != NULL) {
printf("%s %p --> ", currentPtr->data, ¤tPtr);
currentPtr = currentPtr->nextPtr;
}
puts("NULL\n");
}
}
这是将每个单词保存在节点中的功能
void insert(ListNodePtr *sPtr, char *value)
{
ListNodePtr newPtr = malloc(sizeof(ListNode)+1); // create node
if (newPtr != NULL) { // is space available
newPtr->data= malloc(strlen(value));
strcpy(newPtr->data, value);
newPtr->nextPtr = NULL; // node does not link to another node
ListNodePtr previousPtr = NULL;
ListNodePtr currentPtr = *sPtr;
// loop to find the correct location in the list
while (currentPtr != NULL) {
previousPtr = currentPtr; // walk to ...
currentPtr = currentPtr->nextPtr; // ... next node
}
// insert new node at beginning of list
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else { // insert new node between previousPtr and currentPtr
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf("Not inserted. No memory available.\n" );
}
}
解决方案
¤tPtr
给你一个指向指针变量的指针,它不是currentPtr
实际指向的地方。的值¤tPtr
不会在循环中改变,因为变量本身不会改变位置。
如果要打印currentPtr
指向节点本身的位置,请打印 plain currentPtr
。
推荐阅读
- parallel-processing - 分发调度程序作业记录的处理
- emacs - 计算数字参数之前的通用参数的数量
- android - Android自定义适配器不显示所有项目
- error-handling - 我需要帮助重构 Rust 中的错误处理
- c - 为什么地址栏中的 UTF-8 Unicode 与 GET 形式在 C 中有所不同?
- javascript - 基于键从 HTML 和 JavaScript 中的 excel 电子表格中获取行
- javascript - 如何在 React-native 中使用 Picker 设置状态?
- android - 检测到不一致,在执行拉动刷新和滚动列表时回收视图
- azure - Azure 中的 VM 映像和快照有什么区别?
- dialogflow-es - Dialogflow 代理上的密码:Telegram 和 Facebook