0

I am practicing to build a struct data structure in C, and the struct has a field pointer to function. I have written up the following code, but when I tried to compile with gcc, I got the following error:

In function ‘main’: custhreadsTest1.c:27:16: warning: assignment from incompatible pointer type [enabled by default]

Whats the correct way of assigning a function pointer to the pointer field in my struct? Thanks a lot!

#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>

typedef struct
{
  ucontext_t (*gt_context);
  void (*function)(void *);
  int status_flag;
} custhread_t;

void *add(void *)
{
//  do something here
}

int main(void)
{
  ucontext_t ctx_main;
  getcontext(&ctx_main);

  custhread_t th1;
  th1.gt_context = &ctx_main;
  th1.function = &add;
  th1.status_flag = 1;

  return 0;

}
5
  • 1
    Your function pointer is defined as taking a void*, but the function you're assigning to it takes void. Commented Feb 4, 2014 at 23:36
  • thanks @ooga. I've updated the function in my code, but still same error. Commented Feb 4, 2014 at 23:39
  • You have to give a parameter name,e.g. void *add(void* p1) Commented Feb 4, 2014 at 23:40
  • now different return type. Commented Feb 4, 2014 at 23:41
  • 1
    Now your return type is wrong. Should just be void. Commented Feb 4, 2014 at 23:50

2 Answers 2

3

You're assigning it using the correct syntax (the & is optional), but the type of function pointer you are assigning is incompatible with the function pointer type in the struct (your add function takes void *, but your function pointer in the struct doesn't take any arguments).

Your add function should simply be:

void add(void)
{
    // do something
}

Alternatively, if your add function does take a pointer to void as an argument, then:

typedef struct
{
    ucontext_t (*gt_context);
    // pointer to a function that takes a void pointer, no return value
    void (*function)(void *); 
    int status_flag;
} custhread_t;


void add(void *somePointer)
{
    // do something with somePointer
}

And so therefore, if your add function takes and returns a void pointer, then:

typedef struct
{
    ucontext_t (*gt_context);
    // pointer to a function that takes and returns a void pointer
    void *(*function)(void *);
    int status_flag;
} custhread_t;


void *add(void *somePointer)
{
    // do something with somePointer
    // return a void pointer
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much. Your answer works for me. But a question regarding your saying "but your function pointer in the struct doesn't take any arguments)." The function pointer in the struct has (void *) as argument, is (void *) not an argument in this case?
@TonyGW: Yes, if your function pointer in the struct has (void *) as an argument, then you can assign the address of any function that has a void * as an argument. For two function types to be compatible, they must take the same number and type of arguments and return the same type too.]
thank you very much! I got what you are saying, very helpful! :)
1

You're doing it right. Just make sure the function pointer types match. For example, remove the '*' from the struct so that the argument is just void rather than a void pointer. Like this.

typedef struct
{
    ucontext_t (*gt_context);
    void (*function)(void);
    int status_flag;
} custhread_t;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.