CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Function Pointer -> input()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stinky



Joined: 05 Mar 2012
Posts: 99
Location: Central Illinois

View user's profile Send private message

Function Pointer -> input()
PostPosted: Thu Apr 01, 2021 7:57 am     Reply with quote

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: 19219

View user's profile Send private message

PostPosted: Thu Apr 01, 2021 8:38 am     Reply with quote

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. Sad
stinky



Joined: 05 Mar 2012
Posts: 99
Location: Central Illinois

View user's profile Send private message

PostPosted: Thu Apr 01, 2021 9:12 am     Reply with quote

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: 19219

View user's profile Send private message

PostPosted: Thu Apr 01, 2021 10:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 01, 2021 11:02 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group