Frank
Frank

Reputation: 11

Confused on pointer and parentheses syntax

Trying to decipher the following function. I know pCfg is a pointer of any type but I'm not sure what *(AppBIACfg_Type**)pCfg = &AppBIACfg; is assigning to the address of &AppBIACfg.

AD5940Err AppBIAGetCfg(void *pCfg)
{
  if(pCfg){
    *(AppBIACfg_Type**)pCfg = &AppBIACfg;
    return AD5940ERR_OK;
  }
  return AD5940ERR_PARA;
}

Upvotes: 1

Views: 46

Answers (2)

user13244731
user13244731

Reputation:

Confused on pointer and parentheses syntax

  • (AppBIACfg_Type**)pCfg: Tyṕe casts whatever type is pCfg to a AppBIACfg_Type double pointer.

  • *(AppBIACfg_Type**)pCfg: (Note the start star); Dereferences that double pointer, retrieving a single pointer.

  • ... = &AppBIACfg: Assigns the memory address of AppBIACfg to the single pointer.

So...

That expression assigns the memory address of AppBIACfg to a AppBIACfg_Type pointer, this type casted from a void one.

Upvotes: 0

Barmar
Barmar

Reputation: 780879

It's taking the address of the global AppBIACfg variable and storing that in the memory that pCfg points to. The cast is necessary for type correctness.

So if you have something like:

AppBIACfg_Type *foo;
AppBIAGetCfg(&foo);

it's doing the equivalent of

foo = &AppBIACfg;

See Changing address contained by pointer using function

Upvotes: 2

Related Questions