C: Function pointer inside a typedef struct

后端 未结 3 750
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 16:29

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.

<         


        
3条回答
  •  臣服心动
    2021-02-04 17:13

    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.

提交回复
热议问题