calling a function with the variable as a function name please help

This is a discussion on calling a function with the variable as a function name please help within the C Programming forums, part of the General Programming Boards category; Code: /* * eg4.c * * Created on: Aug 23, 2011 * Author: UshaC1 */ /******************************************************************************* ** Include Section ** ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    Question calling a function with the variable as a function name please help

    Code:
    /*
     * eg4.c
     *
     *  Created on: Aug 23, 2011
     *      Author: UshaC1
     */
    /*******************************************************************************
    **                      Include Section                                       **
    *******************************************************************************/
    #include<sys/time.h>
    #include<stdio.h>
    #include<time.h>
    #include<string.h>
    #include"stdlib.h"
    
    
    #define Number_of_Tasks 5  //Number of tasks in a scheduler
    
    
    
    
    /*******************************************************************************
    **            F U N C T I O N    D E C L A R A T I O N S(API)                 **
    *******************************************************************************/
    
    
    
    
    int nTimeInterval;//Global Timer value
    
    void Task_10();
    void Task_20();
    void Task_30();
    void Task_40();
    void Task_50();
    void Task_60();
    void Task_70();
    void Task_80();
    void Task_90();
    
    
    
    void Task_10()
    {
        printf("Task1 at 10ms executing\n");
        printf("Task2 at 10ms executing\n");
    }
    
    void Task_20()
    {
        printf("Task1 at 20ms executing\n");
        printf("Task2 at 20ms executing\n");
    }
    
    void Task_30()
    {
        printf("Task1 at 30ms executing\n");
        printf("Task2 at 30ms executing\n");
    }
    
    void Task_40()
    {
        printf("Task1 at 40ms executing\n");
        printf("Task2 at 40ms executing\n");
    }
    
    void Task_50()
    {
        printf("Task1 at 50ms executing\n");
        printf("Task2 at 50ms executing\n");
    }
    
    void Task_60()
    {
        printf("Task1 at 60ms executing\n");
        printf("Task2 at 60ms executing\n");
    }
    
    void Task_70()
    {
        printf("Task1 at 70ms executing\n");
        printf("Task2 at 70ms executing\n");
    }
    
    void Task_80()
    {
        printf("Task1 at 80ms executing\n");
        printf("Task2 at 80ms executing\n");
    }
    
    void Task_90()
    {
        printf("Task1 at 90ms executing\n");
        printf("Task2 at 90ms executing\n");
    }
    
    void run_task(struct time *val)
    {
        int i,correction;
        float elapsed_time;
        static int j=0;//counter
        j++;
        printf("%d\n",j);
    
        for(i=0;i<Number_of_Tasks;i++)
        {
    
            if(val[i].remaining_time == nTimeInterval)
            {
                clock_t start = clock();        //start a clock
                printf(" Execute %s at %d milliseconds \n ",val[i].task,val[i].period);//calling scheduled task here
                Task_[val[i].period]();// here i am calling a function that is giving error
    
                
    
                elapsed_time=((double)clock() - start) / CLOCKS_PER_SEC; //Time taken to complete a task
    
                /* To check whether my task take more time than nTimeInterval */
                if(elapsed_time > nTimeInterval)
                {
                    printf("Time elapsed: %f\n",elapsed_time);
                    correction=elapsed_time-nTimeInterval;  //correction in time
                    nTimeInterval=nTimeInterval+correction; // adding the correction to nTimeIntervals
                    printf("%d\n",nTimeInterval);
                }
                val[i].remaining_time = val[i].period;
    
    
            }
            else
            {
                val[i].remaining_time -= nTimeInterval;
            }
    
        }
    }

    Here is my code ... in function runtask i need to call my function Task_10,Task_20 or anyother according to the variable var.period so how can i call a function using a variable as a function name ??? .. here i have dome something but its giving error please help me in this concern soon.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Quote Originally Posted by ushacy View Post
    Code:
    #define Number_of_Tasks 5  //Number of tasks in a scheduler
    Macros should be defined with all letters uppercase.
    Last edited by GReaper; 09-26-2011 at 03:04 AM.
    Devoted my life to programming...

  3. #3
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    C is not a scripting language. Thus the function names are not really usable at runtime, when your variables get evaluated.

    You could accomplish this using function pointers - Eg,

    Code:
    void  (*fuptr[9])() = { &Task_10,&Task_20,&Task_30,&Task_40,&Task_50,&Task_60,&Task_70,&Task_80,&Task_90};
    .
    .
    .
     (*fuptr[arrayindexhere])();
    and accessing them based on time. However I'm afraid you need to study a bit more before you get that done safely.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to re-think your logic. I can't imagine why you'd need to do, what you're asking about doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a function named in a variable.
    By JohnAnon in forum C++ Programming
    Replies: 25
    Last Post: 04-01-2011, 12:38 AM
  2. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  3. Replies: 5
    Last Post: 01-12-2006, 11:00 PM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21