【问题标题】:How to access a value pointed by pointer to pointer如何访问指针指向的值
【发布时间】:2021-03-30 18:26:09
【问题描述】:

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

我成功地可以映射顶部和底部块

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

}

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

【问题讨论】:

  • 你展示的内容没有意义。
  • 我有一个内存块分为两半,上半部分将包含值,而 bootm hal 将包含指向上半部分的指针。我想使用指向指针数组的指针访问上半部分
  • 你可能是为什么不直接访问上半部分???这不是我们需要的:(
  • 这似乎在做你想做的事,除了你没有分配适当的内存量。应该是malloc(sizeof(int*) * (250 * 2 + outputOffsetRTI)); 此外,需要有某种方法可以让memBase 在最后释放内存。
  • @siddharthtaunk 啊,您需要将outputOffsetAddress 定义为具有int ** 类型

标签: c pointers


【解决方案1】:

您需要取消引用(例如*ptr)才能通过指针访问值。以下块说明了您提到的访问权限。但是使用三重指针并不是一个好主意:Triple pointers in C: is it a matter of style?

// 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 2012-06-07
    • 1970-01-01
    • 2021-11-18
    • 2023-02-16
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多