c - 如何访问指针指向的值
问题描述
我想访问指向指针数组的指针
我成功地能够映射顶部和底部块
unsigned int *outputAddress = NULL;
unsigned int *outputOffsetAddress = NULL;
unsigned int *OutputptrptrAddr = NULL;
unsigned int *PtrArr[250];
unsigned int **val = PtrArr;
void MemoryMapping(unsigned int outputOffsetRTI)
{
unsigned int *memBase;
memBase = (unsigned int *)malloc(2000);
outputAddress = (unsigned int *)(memBase + outputOffsetRTI);
for (int x = 0; x < 5; x++)
{
*outputAddress = 123;
*outputAddress++;
}
outputAddress = outputAddress - 5;
for (int x = 0; x < 5; x++)
{
PtrArr[x] = (unsigned int *)outputAddress;
outputAddress += 1;
}
outputOffsetAddress = outputAddress + 250;
for (int x = 0; x < 5; x++)
outputOffsetAddress[x] = (unsigned int)PtrArr[x];
}
如何遍历输入指针块以从输入块中获取所有值?
解决方案
您需要取消引用(例如*ptr
)才能通过指针访问值。以下块说明了您提到的访问权限。但是使用三重指针不是一个好主意:C 中的三重指针:是风格问题吗?
// This is the pointer to an array of pointers
unsigned int *** input;
// Dereference it to get the array base
unsigned int ** ptr_array = *input;
// You can use a temporary pointer to iterate over the array
unsigned int * ptr;
// This will be the data pointed to
unsigned int data;
// You need to know the array size as well, assuming it is 5 here
for (int x=0; x<5; x++) {
// The array contains pointers
ptr = ptr_array[x];
// Dereference the pointer to reach the data
data = *ptr;
}
推荐阅读
- node.js - MongoDb:如何仅更新 id 与新对象在数组中推送的对象相同?
- javascript - 未处理的拒绝(错误):Reference.set 失败:第一个参数在属性“signup.test.Robots.props.history.location.state”中包含未定义
- react-native - 按下按钮只会关闭键盘
- typescript - 打字稿 - 参数元组的连接并集
- amazon-web-services - 查询放大graphql多对多关系
- python - 使用循环信号创建伪循环信号
- python - 无法使用 WSL Python 解释器在 PyCharm 中创建虚拟环境
- java - VLCJ - 在 Eclipse 中播放 rtsp 流视频,但不是在制作可执行 JAR 之后
- swift - 打印存储为 SparseMatrixStructure 的矩阵
- javascript - mongodb中的搜索参数