Pointer or reference?

Hi All,

I'd like to use a pointer or reference in the setup section to point to either hour() or hourFormat12() from the Time.h library, rather than the "if" statement that executes repeatedly in the loop (example below). I've tried various syntax to do this, without success.
Please let me know if this is possible, and if so, how it can be done.
Thanks in advance!
Mitch

void loop() {
 if (preset[1]==12) {
    tTime = hourFormat12();
  }
  else
  { 
    tTime = hour();
  }
 displayD(tTime, etc..........);
}

What does displayD() expect the first argument to be? Both hour() and hourFormat12() appear to return int values, so why you need a pointer is a mystery.

If you want to, one time, define a pointer to a function, so that either hour() or hourFormat12() gets called when you call the function by pointer, that is possible. But, in this case it appears to be overkill.

You can assign a pointer to a function quite easily (though slightly cryptically) in C. For example:

void setup()
{
  Serial.begin(9600);
}

int AddTwoNumbers(int a, int b)
{
  return (a+b);
}

void loop()
{
  int (*sum)(int,int); // This is the definition for a function pointer.  Note the parameter types included in brackets and the brackets round the name.
  int answer;

  sum = &AddTwoNumbers;

  answer = sum(5,10); // answer == 15  
  Serial.println(answer);
}
if (preset[1]==12) {
    tTime = hourFormat12();
  }
  else
  { 
    tTime = hour();
  }

If you don't like "if"s, tTime = (preset[1]==12) ? hourFormat12() : hour();
will do the same thing.
Or, as everyone else said, use a function pointer

I think this is easiest:

tTime = (preset[1]==12) ? hourFormat12() : hour();

What I should have mentioned is that I'm trying to eliminate as much code as possible in the loop so as not to impact the refresh rate of the multiplexed nixies. At least this simplifies the code.

Thanks for the help!

Using "?" instead of "if" will have virtually no effect of the execution speed of your code.
If you want to eliminate the conditional, then a function pointer is the way to do it.

I tried following the example, but the following error is thrown:
No matches converting function 'hourFormat12' to type 'int(*)(int)'

Both hour() and hourFormat12() are from the Time.h library, which is included at the beginning of the sketch and used elsewhere without issues. It appears that each has one integer parameter, so I'm not sure what's going on.

Thanks again.

void setup() {

int *(hours) (int);

if (preset[1] ==12) {
    hours = &hourFormat12;
  }
  else
  {
    hours = &hour;
  }

}

cos the declaration you have is for a function returning a pointer to an int.

  • on the name to indicate its a function pointer, not a function
int (*hours) (int);

Thank you. So how can this be made to work?

The asterisk in my example should be inside the parenthesis, that was a typo.

The definition of the pointer needs to match the definition of the function. hour() has no arguments.

Thanks. I think I need an example. I can't get it to work, and I have not yet worked with pointers or references.

int (*hours) (int);

is defining a pointer to a function, using the name hours, that that takes an int argument and returns an int.

The hours function already exists, so the compiler is going to have trouble knowing which function you are referring to when you try to call hours(). You should be using a different name here.

The real hours() function does not take an int argument, so the int in the parentheses is wrong. The parentheses should be empty, just like they are in "int hr = hours();".

I see. It works now. Thanks for the clear explanation!