We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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.
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; }
OMG. I feel even more like a noobie now.
I intended to write:
void Bar(unsigned char *Pointer) { // Access data byte via the pointer } int Foo(unsigned char Arg) { Bar(&Arg); // <<<<<< Is this legal? }
Fortunately you saw my stupidity and answered it with:
Thanks.
This is what a couple of years away from serious programming has done to me :(
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.
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.
"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."
Per, it does not matter whether you use & or not. You will still get the address of the function. In fact many quality analysis tools (e.g. QAC) recommend that you do use the & for clarity.
Yes, I know you can use & - but I don't like it. Some compilers will also emit a warning if & is used.
And I'm not too fond of code analyzer tools that prefers the address-of operator for function pointers.
Using & with arrays specifically means the address of the full array instead of the address of the first element, so there is a different type involved.
Anyway - lots can be said about function pointers and this is an interesting article: stackoverflow.com/.../why-do-all-these-crazy-function-pointer-definitions-all-work-what-is-really-goi
Just read few articles on "pass by value" & "pass by reference" to learn techniques of parameter passing.
explanation with an example www.cprogrammingexpert.com/.../pass_by_value.aspx
an detailed article courses.washington.edu/.../passby.html
function pointer is a concept that must be learnt after learning some basics, regularly used C skills.