This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can I take the address of a function argument?

I know this isn't a Keil specific question, but I know you guys are smart and that I'll just get a direct non-mocking answer.

Is it legal to take the address of a function argument?


void Bar(unsigned char *Pointer)
{
  // Access data byte via the pointer
}

int Foo(unsigned char Arg)
{
  Bar(&Foo); // <<<<<< Is this legal?
}

I know you cannot take the address of a variable of type register and I think that knowledge is clouding my memory.

Parents
  • Understand me correctly.

    The function Bar() can modify the parameter Arg that the function Foo() did send.

    But such a change fill not migrate back to whatever code that did call Foo(). Arg is a local variable inside Foo() and any changes to Foo() (inside Foo() or by the call to Bar()) will just happen to this local Arg. As soon as Foo() ends, the local parameter Arg will be thrown away.

    So yes - you can send the address of parameters to other functions and have these functions change the value of your parameter. The function that takes the pointer doesn't care about if the pointer points to an argument, a local variable or a global variable or even a member of a struct.

Reply
  • Understand me correctly.

    The function Bar() can modify the parameter Arg that the function Foo() did send.

    But such a change fill not migrate back to whatever code that did call Foo(). Arg is a local variable inside Foo() and any changes to Foo() (inside Foo() or by the call to Bar()) will just happen to this local Arg. As soon as Foo() ends, the local parameter Arg will be thrown away.

    So yes - you can send the address of parameters to other functions and have these functions change the value of your parameter. The function that takes the pointer doesn't care about if the pointer points to an argument, a local variable or a global variable or even a member of a struct.

Children
  • So yes - you can send the address of parameters to other functions and have these functions change the value of your parameter.

    The emboldened part is the key part for me.

    Lifetime of variables is not something I'd forgotten about :)

    Thanks for taking the time to answer. I always did like your clear complete responses.