Thread: Can you make an array of commands?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what your declaration of FunArray looks like to the compiler. Note, I added the parentheses in purple that you seem to have forgotten, or that the compiler filled in for you.
    Code:
    int (*FunArray1[8])(int);
    FunArray is an array of 8 pointers to functions that take an int and return an int.

    Here is the declaration of playPT1 you try to assign to your array:
    Code:
    void playPT1(void)
    playPT1 is a function that takes nothing and returns nothing.

    Notice how the purple and red parts don't match up between the two? For proper assignment, function pointers have to match parameter count and types as well as return type. You either need to make FunArray void-void:
    Code:
    void (*FunArray1[8])(void);
    or make your playPT functions int-int:
    Code:
    int playPT1(int x)

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    So when I do this:
    Code:
    *FunArray1[funIndex];
    I am passing a number(funIndex) to (*FunArray1[8]) which means that its input must be int.

    so:
    Code:
    int (*FunArray1[8])(int funIndex);
    So the number passed to the *FunArray1 is the index, so that's why I use "int" and doesn't the *FunArray1 return an address? So shouldn't it's output be an int? (like above)
    But, since the *FunArray1 is going to the index of the function, I don't see why the function, void playPT1(void) should have any int's in either the input or the output of the function.

    However, when I type in "return;" in the function void playPT1(void) Where is it returning? Is it returning to the main program or the *FunArray1?

    And by the way, this is a voice controlled program running on a microcontroller. So, the person is not typing on a keyboard.
    Last edited by ☼Aulos; 07-21-2011 at 01:54 PM.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quote Originally Posted by anduril462 View Post
    Here's what your declaration of FunArray looks like to the compiler. Note, I added the parentheses in purple that you seem to have forgotten, or that the compiler filled in for you.
    Code:
    int (*FunArray1[8])(int);
    FunArray is an array of 8 pointers to functions that take an int and return an int.

    Here is the declaration of playPT1 you try to assign to your array:
    Code:
    void playPT1(void)
    playPT1 is a function that takes nothing and returns nothing.

    Notice how the purple and red parts don't match up between the two? For proper assignment, function pointers have to match parameter count and types as well as return type. You either need to make FunArray void-void:
    Code:
    void (*FunArray1[8])(void);
    or make your playPT functions int-int:
    Code:
    int playPT1(int x)
    Does it matter whether the functions that the array is pointing to return or take values? I wouldn't think so, so I made the the functions void-void.

    What does the input to the functions have to do with its index on the array?

    I thought "int (*FunArray1[8])(int);" took a value (funIndex) and returns an address, the the program goes to the function and when I type in "return;", it goes back to the main program? Am I wrong? ( I realize I probably am wrong about everything I've said).


    Can I use the int "int (*FunArray1[8])(int funIndex);" (its declaration) like this:

    *FunArray1[funIndex];

    to go to the function I want, then, in the function put "return;" to go back to the main program?

    So I guess this is where I am confused especially, why do I have to declare the array like this:
    int (*FunArray1[8])(int funIndex);

    When I am passing a parameter into the array (funIndex) within the []'s?

    Could I declare the function like this:
    int (*FunArray1[int funIndex = 8]; since the []'s is where I am passing the index right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make an array of classes in c#
    By alireza in forum C# Programming
    Replies: 8
    Last Post: 11-16-2010, 05:16 PM
  2. Make an array of arrays
    By JOCAAN in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 12:18 PM
  3. How do I make a *HUGE* array in C++?
    By rooster in forum C++ Programming
    Replies: 10
    Last Post: 02-27-2006, 08:05 PM
  4. how to make an array of strings
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-24-2003, 01:18 PM
  5. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:12 PM

Tags for this Thread