1

I want to make a structure similar to course registration in C. For this, I used struct instead of the 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. 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);

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

I dont understood func pointers clearly. What should I do? Where did I wrong? or can I use func pointer in this situation?

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);
Bahattin
  • 25
  • 5

3 Answers3

2

You declare fp as {

void (*fp[]) () = {

which is an array of pointers to functions returning 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). 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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

The short answer: your function ptr signature is void () and not void* ()

happily celebrating c++ does not care that much about function return types :)

TL;DR;

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. it seems misleading signature, and i'd make the API user call createStudent, createTeacher, etc explicitly.

Some coding style suggestions:

  • typedef your function signature
  • declare your function array (better as member or static to the module)
Tomer W
  • 3,395
  • 2
  • 29
  • 44
  • you are absolutely right but I am trying something different(using func pointers) in this tutorial but i guess i cant :) – Bahattin Dec 22 '20 at 08:23
-3

According to your code, fp is a void, not a void* function. You are always returning nothing from createNode regardless of choice.

Captain Trojan
  • 2,800
  • 1
  • 11
  • 28
  • he is returning `void*` its just written in a convention as `void *createNode()` like variable declarations can be `int* pInt` or `int *pInt` – Tomer W Dec 21 '20 at 21:37
  • @TomerW No, he most definitely is not; as you can hopefully see, `fp` is a function pointer array to functions of type `void`. Whenever you call a function dereferenced from fp, it will return `void`, which he is eventually returning from `createNode`. – Captain Trojan Dec 21 '20 at 21:38
  • my bad, thought you meant the `createNode` function itself.. (so i dont see why it is downvoted) – Tomer W Dec 21 '20 at 21:51