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
  • The first function takes a pointer - so you already got the address of the buffer where you are expected to store the answer.

    The second function takes an int - that is a local copy so you may take the address of this local variable Arg but can't return back a change.

    But note that your code didn't take the address of an argument but instead tried to get the address of the function itself - and you shouldn't use any & to get the address of a function. The function name itself represents the pointer to that function.

    The following will use a function pointer:

    void Bar(int (*fnk)(unsigned char)) {
        fnk('A');
    }
    
    int Foo(unsigned char Arg){
        return Arg + 1;
    }
    
    void main() {
        Bar(Foo);
        while (1) ;
    }
    

    And this code actually uses the address of a parameter:

    void Bar(unsigned char *Pointer) {
        // Access data byte via the pointer
        *Pointer = *Pointer + 1;
    }
    
    int Foo(unsigned char Arg) {
        Bar(&Arg);    // Let Bar() play with our Arg and modify it.
        return Arg;
    }
    

Reply
  • The first function takes a pointer - so you already got the address of the buffer where you are expected to store the answer.

    The second function takes an int - that is a local copy so you may take the address of this local variable Arg but can't return back a change.

    But note that your code didn't take the address of an argument but instead tried to get the address of the function itself - and you shouldn't use any & to get the address of a function. The function name itself represents the pointer to that function.

    The following will use a function pointer:

    void Bar(int (*fnk)(unsigned char)) {
        fnk('A');
    }
    
    int Foo(unsigned char Arg){
        return Arg + 1;
    }
    
    void main() {
        Bar(Foo);
        while (1) ;
    }
    

    And this code actually uses the address of a parameter:

    void Bar(unsigned char *Pointer) {
        // Access data byte via the pointer
        *Pointer = *Pointer + 1;
    }
    
    int Foo(unsigned char Arg) {
        Bar(&Arg);    // Let Bar() play with our Arg and modify it.
        return Arg;
    }
    

Children
More questions in this forum