View previous topic :: View next topic |
Author |
Message |
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
Function Pointer -> input() |
Posted: Thu Apr 01, 2021 7:57 am |
|
|
Is there something special about input()???
This produces the error Line 8, Expecting a (
Code: | #include "16LF1829.h"
#device adc = 10
#define OSCILLATOR 16000000
#use delay (internal = OSCILLATOR)
int main() {
typedef short (*pinReader)(int pin);
pinReader foo = input;
return foo(PIN_A0) ? 1 : 0;
}
|
However, if I wrap input() inside another function, it will happily compile.
Code: | #include "16LF1829.h"
#device adc = 10
#define OSCILLATOR 16000000
#use delay (internal = OSCILLATOR)
short bar(int pin) { return input(pin); }
int main() {
typedef short (*pinReader)(int pin);
pinReader foo = bar;
return foo(PIN_A0) ? 1 : 0;
}
|
I believe I can go ahead with my solution, but I'm just curious if there's something special about input() that I am unaware of.
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Thu Apr 01, 2021 8:38 am |
|
|
Other than the fact that it needs brackets, and a pin number......
You are calling what is defined as a function (so requires brackets),
without these or the value required in these. |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Thu Apr 01, 2021 9:12 am |
|
|
Are you saying that in order for me to successfully assign input to a function pointer, I need to assign it as
Code: |
funcPtr = input(PIN_A0);
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Thu Apr 01, 2021 10:02 am |
|
|
The problem is that 'input' is not actually a function. It is a internal macro
expansion, which has different meanings according to whether it is called
with a fixed value, or a variable. It is also always 'inline', so can't be 'called'.
To call it as a function, you have to encapsulate this in your own wrapper
function. Doing this (and calling input with a variable), makes the resulting
code massively larger than the 'fixed' version. |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Thu Apr 01, 2021 11:02 am |
|
|
Quote: | Is there something special about input()??? |
Quote: | The problem is that 'input' is not actually a function. It is a internal macro expansion |
Thank you for answering my question.
Is this fact documented in the manual?
Regarding: Quote: | Doing this (and calling input with a variable), makes the resulting
code massively larger than the 'fixed' version. |
I see the increase in size. Thank you.
Code: | #include "16LF1829.h"
#device adc = 10
#define OSCILLATOR 16000000
#use delay (internal = OSCILLATOR)
int main() {
return input(PIN_A0);
}
// ROM used: 23 words (0%)
// Largest free fragment is 2048
// RAM used: 5 (0%) at main() level
// 16 (2%) worst case
// Stack used: 0 locations
// Stack size: 16
|
Code: | #include "16LF1829.h"
#device adc = 10
#define OSCILLATOR 16000000
#use delay (internal = OSCILLATOR)
int main() {
int pin = PIN_A0;
return input(pin);
}
// ROM used: 93 words (1%)
// Largest free fragment is 2048
// RAM used: 6 (1%) at main() level
// 21 (2%) worst case
// Stack used: 0 locations
// Stack size: 16
|
|
|
|
|