This question is based on the following C example, which I made for this post:
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t temp;
uint32_t temp2;
} Foo_t;
static void doFoo(const Foo_t *fooAPtr, const Foo_t **fooBPtr);
static void doFoo(const Foo_t *fooAPtr, const Foo_t **fooBPtr)
{
/* Assinging B to A just as an example */
*fooBPtr = fooAPtr;
}
int main()
{
Foo_t fooA = {1, 2};
Foo_t *fooAPtr = &fooA;
const Foo_t *fooBPtr = 0;
doFoo(fooAPtr, &fooBPtr);
printf("\n FooAPtr == %p, FooBPtr == %p \n", fooAPtr, fooBPtr);
printf("\n FooAPtr == %i %i |_| FooBPtr == %i %i \n", fooAPtr->temp, fooAPtr->temp2, fooBPtr->temp, fooBPtr->temp2);
return 0;
}
This code can be run at the link below:
https://onlinegdb.com/rJdHGFNYB
I am questioning my pointer logic and wanted to verify my assumptions about the code. This is my interpretation of the doFoo() function above:
fooAPtr == A constant pointer, pointing to the memory location of a structure of type Foo_t. The const denotes that the address of that memory location should not be changed
fooBPtr == A mutable pointer, pointing to an address that points to the memory location of a Foo_t structure. The const denotes that the address being pointed to cannot change?
It’s pathetic that I am asking this, but I can’t wrap my head around the particular use case. The code works as expected, but that doesn’t mean it’s correct.
So, the question is, is my interpretation of the pointer logic above correct? Specifically, my use of const, and the layers of indirection for fooBPtr. For context, the goal of doFoo() is to assign fooBPtr to fooAPtr via function parameters, not by returning anything.