c - C Void Pointer Inquiry - 不明白为什么我的代码没有按我的预期工作:(
问题描述
谁能向我解释为什么 printf("%d\n", (int *)p[0]) 不打印 10?
void *p[10] = {NULL};
int a = 10;
p[0] = &a;
printf("%d\n", (int *)p[0]);
解决方案
因为您将数组中的指针解释为指向 int ( (int *)p[0]
) 的指针,而不是取消引用它,所以改为*(int *)p[0]
推荐阅读
- node.js - 即使在我按照其他 SO 答案在 NPM CONFIG 中进行了建议的更改之后,如何在代理后面安装 NPM 依赖项?
- python - 在 Luigi 中创建动态顺序任务
- c# - 为什么这个 TryParseExact 表单字符串到 DateTime 失败?
- javascript - 基于存储在字符串数组js中的键的自定义排序/排序对象数组
- javascript - 如何订阅 ReactReduxContext.Consumer 中的更新?
- flutter - 在flutter中使用shared_preferences将用户定义的类存储在磁盘上
- r - r: ifelse() 基于 as.character 的最后一位是奇数还是偶数
- python - usercustomize.py 已加载但在 Mac 上被忽略
- python - 有没有办法让这个 Python Tkinter 函数显示具有某种延迟效果的数字?
- git - Git 选项卡完整显示所有 Git 命令历史记录