c - 关于指针的问题
问题描述
我想将 hashnode 与节点链接起来。由于hashnode
是Node**
(指向节点指针的指针),因此 的值hashnode
应该是节点指针的地址(即&node
)。我的代码如下。
但是,它向我显示了一个错误,即分配给struct Node *
from Node **
(aka struct Node **
) 的不兼容指针类型;消除 &。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Hashnode
{
int size;
struct Node** hashnode;
}Hashtable;
typedef struct Node
{
char* word;
struct Node* next;
}Node;
int main(void)
{
Node* node = malloc(sizeof(Node));
node->word = "Elvin";
node->next = NULL;
printf("First Node created successfully!...\n");
Hashtable hasht;
hasht.size = 10;
hasht.hashnode = malloc(sizeof(*hasht.hashnode)*hasht.size);
for (int i = 0; i < hasht.size; i++)
{
hasht.hashnode[i] = NULL;
printf("the address of the %i hashnode is: %p\n", i, hasht.hashnode[i]);
}
printf("The hashtable is created successfully!...\n");
int key = 3;
hasht.hashnode[key] = &node;
}
知道我做错了什么吗?我在概念上错了什么?
解决方案
知道我做错了什么吗?我在概念上错了什么?
虽然hashnode
是指向 a 的指针struct Node
,hashnode[key]
但只是指向 a 的指针struct Node
。但是&node
又是指向 a 的指针的地址(或指针)struct Node
。因此,以下分配失败:
hasht.hashnode[key] = &node;
编译器正确地抱怨。
您必须执行以下操作:
hasht.hashnode[key] = node;
推荐阅读
- angular - 如何修复角度获取请求中的“确定”消息
- jsp - 我们想为 JSP 页面的 i18n 使用 JSTL FMT 标签,但将语言代码文件夹保留在 url 中以用于 SEO 目的
- mysql - 如何快速在 MySQL 中的多个列中进行全文搜索?
- regex - 在 powershell 脚本中使用正则表达式替换键/值标签的值
- excel - 根据另一列中的值提取唯一值以分隔工作表
- c - 编译器不正确的注册表 %rax C 代码问题
- java - 重新安装 Windows 后出现 Cordova 错误
- reactjs - 如何创建 ReactJS 可缩放图像灯箱
- node.js - Node.js | 当ioHook被key hook激活时触发PUG模板中的函数?
- haskell - 绑定如何在“case of”块中隐藏现有绑定?