简体   繁体   中英

Using function pointer array in pthread_create - near initialization

EDIT: This has been answered with the help of John Bollinger and alk. Read their comments below! I've also included my revised code at the bottom on this original post for anyone searching to read

I'm trying to use an array of function pointers to send as the function argument in pthread_create but when I try to compile (using: "gcc -std=c99 P1.c -lpthread" I am getting the following errors:

P1.c:40:1: warning: initialization from incompatible pointer type [enabled by default]

func_ptr funcs[4] = {func0, func1, func2, func3};

P1.c:40:1: warning: (near initialization for 'funcs[0]') [enabled by default]

P1.c:40:1: warning: initialization from incompatible pointer type [enabled by default]

P1.c:40:1: warning: (near initialization for 'funcs[1]') [enabled by default]

P1.c:40:1: warning: initialization from incompatible pointer type [enabled by default]

P1.c:40:1: warning: (near initialization for 'funcs[2]') [enabled by default]

P1.c:40:1: warning: initialization from incompatible pointer type [enabled by default]

P1.c:40:1: warning: (near initialization for 'funcs[3]') [enabled by default]

I think it's an issue with my typedef declaration of my function pointer but I'm having trouble figuring out exactly what the issue is. Below is the relevant code snippet:

void func0()
{ printf("A"); }

void func1()
{ printf("B"); }

void func2()
{ printf("C"); }

void func3()
{ printf("D"); }

typedef void* (*func_ptr)(void *);
func_ptr funcs[4] = {func0, func1, func2, func3};

int main(int argc, char *argv[])
{
    pthread_t pth[THREADCNT];

    for(int i =0; i < THREADCNT; i++)
            pthread_create(&pth[i], NULL, (funcs[i])(), NULL);

    for(int i =0; i < THREADCNT; i++)
            pthread_join(pth[i], NULL);
}

REVISED WORKING CODE BELOW

// changed these functions to void* with (void * pv) parameter to
// match the typedef (and also match the pthread_create parameter)
void* func0(void * pv)
{ printf("A"); }

void* func1(void * pv)
{ printf("B"); }

void* func2(void * pv)
{ printf("C"); }

void* func3(void * pv)
{ printf("D"); }

typedef void* (*func_ptr)(void *);
func_ptr funcs[4] = {func0, func1, func2, func3};

int main(int argc, char *argv[])
{
    pthread_t pth[THREADCNT];

    for(int i =0; i < THREADCNT; i++)
            pthread_create(&pth[i], NULL, funcs[i], NULL); 
// changed the funcs[i] because the function isn't called, the value is just passed as an argument

    for(int i =0; i < THREADCNT; i++)
            pthread_join(pth[i], NULL);

}

Change

void func0()

to be

void * func0(void * pv)

(same for the other three)

because that's the type the array is defined to hold, as well as the type pthread_create() expects.


Change

pthread_create(&pth[i], NULL, (funcs[i])(), NULL);

to be

pthread_create(&pth[i], NULL, funcs[i], NULL);

as you do not want to call funcs[i] but pass its value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM