Thread: Dynamic populate array of functions

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    31

    Dynamic populate array of functions

    Hi everyone,

    I have created a function array. My question is, is it possible to dynamically populate the function array based on some criteria each time? I want to have a loop that will check some conditions and will adjust the function array accordingly. Thanks in advance

    Code:
    typedef void (*ReadSensor)(SensorResult *); //Create a function pointer datatype with the alias ReadSensor that returns void(nothing) and take as parameter structure pointer (SensorResult)
    
    
    
    
    //Create three function prototypes that returns nothing taking as parameter the struct pointer SensorResult
    void floatFunction(SensorResult *);
    void byteFunction(SensorResult *);
    void longFunction(SensorResult *);
    
    
    ReadSensor sensorFunctions[] = { floatFunction, byteFunction, longFunction }; //Manually populate the array of fuction pointers with the functions addresses

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,744
    Yes, you can assign function pointers to the elements of an array of function pointers at run time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    31
    Quote Originally Posted by laserlight View Post
    Yes, you can assign function pointers to the elements of an array of function pointers at run time.
    I tried to assign but it didn't work. I tried this

    Code:
    ReadSensor sensorFunctions[0] =  floatFunction; 

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,744
    That's because assignment looks like this:
    Code:
    sensorFunctions[0] =  floatFunction;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't populate my array
    By k31453 in forum C Programming
    Replies: 5
    Last Post: 05-20-2013, 03:44 AM
  2. Populate An Array With Numbers
    By mcertini in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2011, 11:35 AM
  3. populate array problem
    By DaleZ in forum C# Programming
    Replies: 3
    Last Post: 11-24-2010, 01:41 AM
  4. populate an array struct
    By flipguy_ph in forum C Programming
    Replies: 10
    Last Post: 04-17-2009, 04:07 PM
  5. FOR loop to populate a 2D array
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-05-2008, 10:45 AM

Tags for this Thread