C:指针在结构体功能 [英] C: pointer to a function in Struct

查看:151
本文介绍了C:指针在结构体功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在练建立在C结构的数据结构,该结构有一个字段函数指针。我已经写了下面的code,但是当我试图用gcc来编译,我得到了以下错误:

在函数'主':
custhreadsT​​est1.c:27:16:警告:从兼容的指针类型[默认启用]分配

什么在我的结构分配函数指针的指针域的正确方法是什么?非常感谢!

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&ucontext.h GT;typedef结构
{
  ucontext_t(* gt_context);
  无效(*功能)(无效*);
  INT status_flag;
} custhread_t;void *的增加(无效*)
{
//这里做什么
}INT主要(无效)
{
  ucontext_t ctx_main;
  的getContext(安培; ctx_main);  custhread_t TH1;
  th1.gt_context =安培; ctx_main;
  th1.function =安培;添加;
  th1.status_flag = 1;  返回0;}


解决方案

您正在使用正确的语法(即&安培; 是可选的)分配给它,但类型功能要分配的指针是在结构中的函数指针类型不兼容(你的添加函数需要无效* ,但在结构中的函数指针不带任何参数)。

添加函数应该仅仅是:

 无效添加(无效)
{
    // 做一点事
}

另外,如果你的添加函数的确实的一个指针为void作为参数,则:

  typedef结构
{
    ucontext_t(* gt_context);
    //指针,需要一个空指针的函数,没有返回值
    无效(*功能)(无效*);
    INT status_flag;
} custhread_t;
无效添加(无效* somePointer)
{
    //做一些somePointer
}

等于是,如果你的添加函数需要的返回的空指针,则:

  typedef结构
{
    ucontext_t(* gt_context);
    //指针,采取并返回一个空指针的函数
    无效*(*功能)(无效*);
    INT status_flag;
} custhread_t;
void *的增加(无效* somePointer)
{
    //做一些somePointer
    //返回一个空指针
}

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;

}

解决方案

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
}

这篇关于C:指针在结构体功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆