简体   繁体   English

在 C 中使用带 void 的 function 指针

[英]Use function pointer with void in C

I want to make a structure similar to course registration in C.我想做一个类似于 C 中的课程注册的结构。 For this, I used struct instead of the table.为此,我使用了 struct 而不是 table。

STUDENT *createStudent(){
    return (STUDENT *) malloc(sizeof(STUDENT));
}

TEACHER *createTeacher(){
    return (TEACHER *) malloc(sizeof(TEACHER));
}

COURSE *createCourse(){
    return (COURSE *) malloc(sizeof(COURSE));
}

COURSEREGISTRATION *createCourseRegistration(){
    return (COURSEREGISTRATION *) malloc(sizeof(COURSEREGISTRATION));
}

I want to pass these functions to a variable with a function pointer.我想将这些函数传递给带有 function 指针的变量。 Like;喜欢;

void *createNode(int choise){
    void (*fp[]) () = {createStudent, createTeacher, createCourse, createCourseRegistration};
    return (fp[choise] ());
}

I want to do malloc with func pointers and get an error in this function (main func);我想用函数指针malloc并在这个 function (main func) 中出错;

STUDENT *studentHead = NULL;
void *temp = studentHead;
temp = createNode(0);//Zero for student

I dont understood func pointers clearly.我不清楚 func 指针。 What should I do?我应该怎么办? Where did I wrong?我哪里错了? or can I use func pointer in this situation?或者我可以在这种情况下使用 func 指针吗?

THX谢谢

EDIT: I solved the problem like this;编辑:我解决了这样的问题;

#define CREATESTUDENT 0
#define CREATETEACHER 1
#define CREATEDCOURSE 2
#define CREATECOURSEREGISTRATION 3

void createNode(void **temp, int choise){
    switch(choise){
        case CREATESTUDENT:
            *temp = (STUDENT *) malloc(sizeof(STUDENT));
            break;
        case CREATETEACHER :
            *temp = (TEACHER *) malloc(sizeof(TEACHER));
            break;
        case CREATEDCOURSE :
            *temp = (COURSE *) malloc(sizeof(COURSE));
            break;
        case CREATECOURSEREGISTRATION :
            *temp = (COURSEREGISTRATION *) malloc(sizeof(COURSEREGISTRATION ));
            break;
    }
}

and used call func;并使用了调用函数;

STUDENT *temp = studentHead;
createNode(&temp, CREATESTUDENT);

You declare fp as {您将fp声明为 {

void (*fp[]) () = {

which is an array of pointers to functions returning void.这是一个指向返回 void 的函数的指针数组。 You want functions that return pointers, so you want你想要返回指针的函数,所以你想要

void *(*fp[])() = {

but then you still have the problem that the function pointers you are using to initialize this array are the wrong type -- they are all functions that return pointers to a real type (not void).但是你仍然有一个问题,你用来初始化这个数组的 function 指针是错误的类型——它们都是返回指向真实类型(不是 void)的指针的函数。 So while this will probably work, it is actually undefined behavior as far as the standard is concerned.因此,虽然这可能会起作用,但就标准而言,它实际上是未定义的行为。

If you change all your create functions to return void * instead of STUDENT * (and similar), then it would be safe.如果您将所有create函数更改为返回void *而不是STUDENT * (和类似的),那么它将是安全的。

The short answer: your function ptr signature is void () and not void* ()简短的回答:您的 function ptr 签名是void ()而不是void* ()

happily celebrating c++ does not care that much about function return types:)高兴地庆祝 c++ 不太关心 function 返回类型:)

TL;DR; TL;博士;

Why make life harder than they need to be?为什么要让生活变得比他们需要的更艰难? why not use simple switch为什么不使用简单的开关

void *createNode(int choice){
  switch(choice){
    case 0:
      return createStudent();
    case 1:
      return createTeacher();
    ...
    ...
  }
}

Note: I don't see the point of creating createNode from the first place.注意:我没有看到从一开始就创建createNode的意义。 it seems misleading signature, and i'd make the API user call createStudent , createTeacher , etc explicitly.它似乎具有误导性的签名,我会明确地让 API 用户调用createStudentcreateTeacher等。

Some coding style suggestions:一些编码风格建议:

  • typedef your function signature typedef 你的 function 签名
  • declare your function array (better as member or static to the module)声明您的 function 数组(最好作为成员或 static 到模块)

According to your code, fp is a void , not a void* function.根据您的代码, fpvoid ,而不是void* function。 You are always returning nothing from createNode regardless of choice .无论choice如何,您总是不会从createNode返回任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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