Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Function pointer inside a typedef struct

I am trying to create a linked list in C but trying to pack it nicely in somewhat of a C++ style class. I am having some issues however using function pointers in C.

typedef struct linkedList {
    int count;
    struct msgNode *front;
    struct msgNode *back;
    void (*addMSG)(unsigned char *, int, struct linkedList *);
} msgList;

void addMSG(unsigned char *data, int size, struct linkedList *self);

Ideally, I would like to have it such that you can make you list and then to add you can simply call a "method"(function) within the structure, simulating behavior you would see in C++.

Currently I get a segmentation fault when I call addMSG, which of-course is because addMSG is not pointing to a function. However, I don't want to have to specify a function to point to every single time I want use a linked list. Is there any nice way to have function pointers without implicitly having to point to the function, or do you have to implicitly point it to the function?

This is only the partial implementation shown here. At the end, this struct will have all the necessary functions. This is just for the sake of keeping this question short and to the point.

like image 214
Edwin Avatar asked Sep 26 '13 21:09

Edwin


People also ask

Can you have a function pointer in a struct?

Function pointers can be stored in variables, structs, unions, and arrays and passed to and from functions just like any other pointer type. They can also be called: a variable of type function pointer can be used in place of a function name.

Can we declare pointer inside structure in C?

Structure pointer in C is declared using keyword struct followed by structure name to which pointer will point to follower by pointer name. A structure pointer can only hold the address of a variable of the same structure type used in its declaration.

Can we declare pointer inside structure?

The declaration of a structure pointer is similar to the declaration of the structure variable. So, we can declare the structure pointer and variable inside and outside of the main() function.


3 Answers

You need to assign the function to the member. i also recommend giving them different names:

typedef void (*addMSGFunc)(unsigned char *, int, struct linkedList *);

typedef struct linkedList {
    int count;
    struct msgNode *front;
    struct msgNode *back;
    addMSGFunc addMSG;
} msgList;

void addMSGImpl(unsigned char *data, int size, struct linkedList *self)
{
    ...
}

And then after creating a msgList:

msgList myList;
myList.addMSG = addMSGImpl;
like image 188
Asaf Avatar answered Nov 06 '22 01:11

Asaf


Well you can't add a default value in the declaration of the struct but what you can do is:

  • Create a function to initialize the linkedList instance - I guess you've seen that in C style libraries
  • Create a default list item and use that when creating new entities.

Like:

void addMSG(unsigned char *data, int size, struct linkedList *self);

struct linkedList {
    int count;
    struct msgNode *front;
    struct msgNode *back;
    void (*addMSG)(unsigned char *, int, struct linkedList *);
} DefaultList = {0, NULL, NULL, addMSG};
like image 40
Rudolfs Bundulis Avatar answered Nov 06 '22 01:11

Rudolfs Bundulis


You can have an uninitialized function pointer just fine as long as you don't actually use it. If you do want to use it to call a function, then obviously you have to assign a function to it. C is unable to guess which function you want to use.

If you have a linked list structure where sometimes you need a function, and sometimes you don't, then just assign NULL to it when you create the list, and have your list implementation only call the function when it's not NULL.

If it always points to the same function, then just do the assignment in your constructor function.

like image 1
Crowman Avatar answered Nov 06 '22 03:11

Crowman