首页 > 解决方案 > 如何访问指针指向的值

问题描述

在此处输入图像描述

我想访问指向指针数组的指针

我成功地能够映射顶部和底部块

    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];

}

如何遍历输入指针块以从输入块中获取所有值?

标签: cpointers

解决方案


您需要取消引用(例如*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;
}

推荐阅读