Re: Creating Array of Function pointers dynamically

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Vamsi Krishna <krishna_k@students.iiit.net> [2003-05-29 20:21]:
> Hi ,
> 
>     Iam stuck up with the following problem during modification of a part
> of the kernel code.  The problem is to create an "Array of function
> pointers dynamically". I tried to solve the problem in this way.
> 
> -----------------------------------------------------------------------
> 
> int * function(); /*declaration*/
> 
> int *(**array_of_function_pointers)(); /*declaration of function pointer 
> 					array*/
> 
> /*The sizeof the array ie (array_of_function_pointers) is not know 
> before hand so I need to create dynamically.
>  
> If the size of array is known then 
> "int *(*array_of_function_pointers[SIZE])()" works
> */
> 
> /*Allocating memory for array_of_function_pointers */
> 
> array_of_function_pointers = (XXXX) malloc(sizeof(YYYY)*SIZE);
>                              ------              -----
>   
> array_of_function_pointers[1]=function;
> 
> (*array_of_function_pointers[1])(); /*function call*/
> 
> -----------------------------------------------------------------
> 
> what should i keep in the place of XXXX and YYYY in the above code.
> 

well, I assume this is rather a basic C programming problem than Linux
Kernel related stuff.  Anyway:

typedef int *(*function_t)(void);  /* assuming you would want to return
                                    a pointer to an int */

function_t *function_array;  

function_array = (function_t *)malloc(sizeof(function_t) * size_of_array);
/* btw, a function pointer is just a void pointer */
 

Please correct if mistaken.


wbr,
Lukas
-- 
Lukas Ruf           | Wanna know anything about raw |
<http://www.lpr.ch> | IP?  <http://www.rawip.org>   |
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux