Structure Array of Function Pointers

Hi i'm relatively new to arduino, and above beginner at programing.
I saw that you can store functions in an array, visited some other forums and saw that this is (I think) how you should do it.
Yet im having a problem.

typedef void (* Pointer)();
Pointer states[4]={&motorState0,&motorState1,&motorState2,&motorState3};
float maxSpeed=1;
float currentSpeed;
float currentTask;
int timeCount=0;
int currentMotorState;
int stateTimes[4];

void setup() {
  for(int a=2;a<14;a++){
    pinMode(a,OUTPUT);
    digitalWrite(a,0);
  }
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  Serial.begin(9600);
  currentMotorState=0;
  stateTimes[4]=new int[5000,10000,15000,0];
}

void loop() {
  motorControl();
  delay(1);
}

void motorControl(){
  timeCount++;
  states[currentMotorState];
  if(timeCount==stateTimes[currentMotorState]){
    currentMotorState++;
    if(timeCount==stateTimes[currentMotorState])
    currentMotorState++;
  }
  Serial.println(0);
}

void motorState0(){
  currentSpeed=maxSpeed*timeCount/stateTimes[0];
  currentTask+=currentSpeed;
  Serial.println(1);
}

void motorState1(){
  currentTask+=currentSpeed;
  Serial.println(2);
}

void motorState2(){
  delay(1);
}

void motorState3(){
  delay(1);
}

When i open the serial i can only see "0", not one "1". While it should be "0" than "1" for the first 5 seconds.
I tried to use this

void motorControl(){
  timeCount++;
  states[currentMotorState](); <---------------------------------
  if(timeCount==stateTimes[currentMotorState]){
    currentMotorState++;
    if(timeCount==stateTimes[currentMotorState])
    currentMotorState++;
  }
  Serial.println(0);
}

but that way in serial i don't see anything. and yet there is no error on the first or second try.

I would really appreciate if i get any kind of reply.
Thanks in advance.

C++ doesn't allow copying arrays with simple assignment so your initialization of stateTimes in setup() will not do what you expect. It is probably saving the address of the newly created array in the first element off the end of your array!

Either initialize in the declaration or assign each element separately in setup().

Pointer states[4]={&motorState0,&motorState1,&motorState2,&motorState3};
Pointer states[4]={motorState0,motorState1,motorState2,motorState3};

Functions are already pointers so you shouldn't need the &
Must see article by Nick Gammon:

Z

zhomeslice:
Functions are already pointers so you shouldn't need the &

Including the ampersand is redundant. But it causes no problems.

It makes your (the programmer's) intent crystal clear to the future you and anyone else who is saddled with maintaining your code.

In other words, there is no valid reason to exclude the ampersand and one valid reason to include it.

[quote author=Coding Badly date=1493486296 link=msg=3240845]
Including the ampersand is redundant. But it causes no problems.

It makes your (the programmer's) intent crystal clear to the future you and anyone else who is saddled with maintaining your code.

In other words, there is no valid reason to exclude the ampersand and one valid reason to include it.[/quote]
This is great to know! Thank you! My understanding of the use of ampersand was clouded because I assumed that the ampersand would always shift to the memory location of the item even when it's a pointer. But what you are saying is that if it is already defined as a pointer nothing changes. Am I correct?

zhomeslice:
But what you are saying is that if it is already defined as a pointer nothing changes. Am I correct?

Yes with a small terminology correction: "...already an address...". The address of an address is an address.

In C(++) a "pointer" is a datatype like "char" and "float". So, the address of a pointer is the address of the pointer.