top of page

[C] Array of function pointers

  • Writer: M.R
    M.R
  • Jul 27, 2024
  • 1 min read

What want to do

As stated in the title, I would like to define an array of function pointers.

Specifically, for example, the third argument of pthread_create is the pointer to the function you want to run in the new thread. When you want to allocate new threads for multiple functions, you can put those pointers into an array and process them together.



Solution

You can do it like this:

void *(*funcs[])(void *)

The meaning is as follows:

(*funcs): function pointers

(*funcs)(void *): A pointer to a function that takes a void pointer as an argument.

void *( *funcs): A pointer to a function that returns void *


Use it like this

void *(*services[])(void *) = {Service_1, Service_2, Service_3};

for (i = 0; i< NUM_THREADS; i++){
	pthread_create(&threads[0], sched_attr, services[i], (void *)threadParams);
}

void *Service_1(void *threadp){
 ...
}

 
 
 

Recent Posts

See All

コメント


category

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Inquiries: Please contact us on Twitter

  • Twitter
bottom of page