0

enter image description here

I want to access the pointer to an array of pointers

I am successfullay able to map the top and the bottom block

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

}

How to tranverse through the input pointer block to get all values from the input block?

siddharth taunk
  • 309
  • 4
  • 12
  • What you showed does not make a sense. – Vlad from Moscow Mar 30 '21 at 18:33
  • I have a memory block divided into two half the top half will contain values and the bootm hal will contain pointers to the top half.. I want to access the top half using a pointer to array of pointers – siddharth taunk Mar 30 '21 at 18:38
  • U might be thing why not access the top half directly??? thats not how we need it :( – siddharth taunk Mar 30 '21 at 18:40
  • 1
    This seems to be doing what you want, aside from the fact that you're not allocating the proper amount of memory. It should be `malloc(sizeof(int*) * (250 * 2 + outputOffsetRTI));` Also, there needs to be some way of getting `memBase` back to free the memory at the end. – dbush Mar 30 '21 at 18:43
  • How to access the value @dbush if you may have seen only the Address are available in the bottom half... How to access value from the top half – siddharth taunk Mar 30 '21 at 18:46
  • 1
    @siddharthtaunk Ah, you need to define `outputOffsetAddress` as having type `int **` – dbush Mar 30 '21 at 18:49
  • Can someone please help me copying the value pointed by the double pointer to an array using memcpy() – SID Apr 01 '21 at 10:58

1 Answers1

0

You need to dereference (e.g. *ptr) to access a value through a pointer. The following block illustrates the access you mention. But using triple pointers is not a good idea: 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;
}
mkayaalp
  • 2,631
  • 10
  • 17