How to properly define a function pointer in struct, which takes struct as a pointer?

后端 未结 4 827
慢半拍i
慢半拍i 2021-01-05 00:56

I have a struct with a callback function, the callback function needs a pointer to the structure in order to do its operation. How do I properly define these elements such t

4条回答
  •  灰色年华
    2021-01-05 01:29

    Adding to the answer by nos above.

    The key insight here is that when dealing with a declaration like "typedef struct name1 {} name2;", you are actually declaring two types i.e. "struct name1 {};" and then "typedef struct name1 name2;", where "struct name1" is a type and you have to use the syntax "struct name1" to refer to it, and "name2" is a type, and you refer to it as "name2". You are allowed to leave "name1" out, in which case you just define the second type and the first one remains a anonymous struct.

    Now, in the first case, if you want to refer to the type "struct pr_PendingResponseItem", you need to declare that type, instead of the anonymous struct you have declared. So, change your struct declaration to "struct pr_PendingResponseItem".

    In the second case, you are trying to refer to a struct type as a forward reference (i.e. referring to it before its definition is complete), which is allowed, but to refer to a struct type, the required syntax is "struct name". So you need to replace forward references to "LinkedItem_" in your definition with "struct LinkedItem_".

提交回复
热议问题