Array of function pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clearner321
    New Member
    • Sep 2015
    • 22

    Array of function pointers

    Small doubt regarding function pointers as you can see the array of function pointers is initialized with two elements, but i am calling up to 3. In such cases what is the behavior of the program? How to avoid using MAX?

    Code:
    void Test1(void);
    void Test2(void);
    #define MAX 3
    for(i = 0; i < MAX; i++)
    {
     (*funcptr[i])();
    }
    void (*funcptr[])(void) =
    {
        Test1,
        Test2,
    };
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, define your array of function pointers. Before you do this you have to know the arguments and return type of the functions whose addresses go in the array elements.

    Second, assign the address of the functions to the array elements. If you have several functions whose addresses you are storing in the array, then MAX would not be needed because you can't use a loop to assign different functions to different elements.

    Show me what your array looks like.

    Comment

    • Clearner321
      New Member
      • Sep 2015
      • 22

      #3
      This is the program i am trying to execute
      Code:
      #include <stdio.h>
      
      #define MAX		3
      void Test1(void);
      void Test2(void);
      
      void (*funp[])(void)=
      	{
      		Test1,
      		Test2,
      	};
      
      int main(void)
      {
      int index=0;
      for(index=0;index <MAX; index++)
      {
         (*funp[index])();	
      }
      
      return 0;
      }
      
      void Test1(void)
      {
      	printf("In test1\n");
      }
      
      void Test2(void)
      {
      	printf("In Test2\n");
      }
      How to avoid using MAX because every time I add new function I have to modify MAX?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You have a loop in your main() that is using an array but there is no array in the program.

        What does your array look like? You can't use the index operator ( [ ] ) unless you have an array.

        Like this:

        Code:
        void(*arr[3])(void);
        This code says that arr is an array of three things which are pointers to functions with void arguments that return void.

        What is your array supposed to contain?

        Comment

        • Clearner321
          New Member
          • Sep 2015
          • 22

          #5
          i thought this an array of function pointers.
          Code:
          void (*funp[])(void)=
          {
            Test1,
            Test2,
          };
          I get proper output if I use MAX equal to 2. I am getting confused.

          Or should i have said it is an array of pointers to function.
          Last edited by Clearner321; Nov 13 '15, 06:09 AM. Reason: Additional information.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Sorry. you are correct. I missed that since I don't usually initialize arrays that way.

            OK so in your loop you crash because MAX is 3 but there are only 2 elements in the array. Just change MAX to 2 and the code will run. Or create an array of 3 elements.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              BTW there is a weirdness here in C and C++.

              The name of a function is the address of the function. So the address of

              Code:
              int myFunc(int);
              is myFunc. You call the function by

              Code:
              myFunc(5);
              There are some people who see myFunc as an address of a function so you would need to *myFunc to access the actual function:

              Code:
              *myFunc(5);
              Because of this both ways of calling a function are supported.

              In your case, the loop could be:

              Code:
              for (index = 0; index <MAX; index++)
              	{
              		funp[index]();
              	}
              which is easier to read without the de-reference operator.

              Comment

              • Clearner321
                New Member
                • Sep 2015
                • 22

                #8
                I plan to modify the code to avoid using MAX. Can I use something like this
                Code:
                #include <stdio.h>
                void Test1(void);
                void Test2(void);
                
                void (*funp[])(void)=
                	{
                		Test1,
                		Test2,
                	};
                
                int main(void)
                {
                int index=0;
                int size = sizeof(funp)/sizeof(funp[0]);
                for(index=0;index < size; index++)
                {
                   (*funp[index])();	
                }
                return 0;
                }
                
                void Test1(void)
                {
                	printf("In test1\n");
                }
                
                void Test2(void)
                {
                	printf("In Test2\n");
                }

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  You can up to a point.

                  The sizeof operator gives you the size of the variable on the stack.

                  In this case you will get the size of two function pointers divided by the size of the function pointer in element 0. The result will be 2 and there are 2 elements in the array.

                  However, if the array is on the heap:

                  Code:
                  void(**funp)(void);
                  	
                  	int index = 0;
                  
                  	funp =  malloc(sizeof(void(*)(void)) * 10);
                  
                  	for (index = 0; index <10; index++)
                  	{
                  		//funp[index]();
                  		funp[index]();
                  		
                  	}
                  all you have is the sizeof a pointer to a function pointer. The array size of 10 elements has been lost.

                  So this:

                  Code:
                  int size = sizeof(funp)/sizeof(funp[0]);
                  will always be 1.

                  Because of this, the common practice is to always have two variables. One for the array and one for the number of elements in the array.

                  Comment

                  Working...