Passing a memory address to a function.

Kuba0040

Well-known member
Hello,
This will take some explaining, so here it goes. I have an array in memory to which I have setup a pointer to called ArrayPointer, so I can pass on the array to other functions:

Code:
int32_t myArray[64]; //Real array in memory
int32_t *ArrayPointer=myArray; //Pointer to the array

Now, one function needs to get the memory address of the first cell in the array. The way I implemented this is as follows:

Here is the example function:
Code:
void test_adress_function(int32_t *LocalPointer)
{
  Serial.printf("64bit hex: 0x%" PRIx64 "\n", &LocalPointer); //Shows the memory adress in HEX
}

And here is how we pass the array to it:
test_adress_function(ArrayPointer);

However, the function shows a different address to when I just check &ArrayPointer.

Why? (I guess that LocalPointer shows the memory adress of the ArrayPointer instead of the real array). And how can I pass on the memory address of the real array in memory to a function from the ArrayPointer?

Thank You for the help.
 
&LocalPointer is the address of the local variable LocalPointer on the stack (and has type int32_t**). Nothing to do with the value it is holding,
which it the pointer you want to print.

Code:
void test_adress_function(int32_t *LocalPointer)
{
  Serial.printf("64bit hex: 0x%" PRIx64 "\n", (uintptr_t)LocalPointer); //Shows the memory adress in HEX
}
I cast to uintptr_t as that's an integer with the same size as pointers in the architecture.

By the way array variables are pointers anyway, just to add confusion, array is the same as &array is the same as &array[0] is the same as array+0
[ put another way arrays are implemented as a pointer to the zeroth element. ]
 
Addendum to MarkT's answer: Instead of casting to an integer type you can also print pointer values directly using the %p format-parameter. (Makes printf friendlier to the eyes...)

Code:
int32_t myArray[64]; // Real array in memory

void test_adress_function(int32_t* localPointer)
{
    Serial.printf("%p\n", localPointer); // Shows pointer value  HEX
}

void setup()
{
    while (!Serial) {}

    Serial.println("Address of myArray:") ;
    test_adress_function(myArray);

    Serial.println("\nAddress of elements:");
    for (int i = 0; i < 64; i++)
    {
        Serial.printf("%02d ", i);
        test_adress_function(&myArray[i]);
    }
}

loop
{
}

Which prints:
Code:
Address of myArray:
0x20001344

Address of elements:
00 0x20001344
01 0x20001348
02 0x2000134c
03 0x20001350
04 0x20001354
05 0x20001358
06 0x2000135c
07 0x20001360
08 0x20001364
09 0x20001368
10 0x2000136c
11 0x20001370
12 0x20001374
13 0x20001378
14 0x2000137c
15 0x20001380
16 0x20001384
17 0x20001388
18 0x2000138c
19 0x20001390
20 0x20001394
21 0x20001398
22 0x2000139c
23 0x200013a0
24 0x200013a4
25 0x200013a8
26 0x200013ac
27 0x200013b0
28 0x200013b4
29 0x200013b8
30 0x200013bc
31 0x200013c0
32 0x200013c4
33 0x200013c8
34 0x200013cc
35 0x200013d0
36 0x200013d4
37 0x200013d8
38 0x200013dc
39 0x200013e0
40 0x200013e4
41 0x200013e8
42 0x200013ec
43 0x200013f0
44 0x200013f4
45 0x200013f8
46 0x200013fc
47 0x20001400
48 0x20001404
49 0x20001408
50 0x2000140c
51 0x20001410
52 0x20001414
53 0x20001418
54 0x2000141c
55 0x20001420
56 0x20001424
57 0x20001428
58 0x2000142c
59 0x20001430
60 0x20001434
61 0x20001438
62 0x2000143c
63 0x20001440
 
Back
Top