Click here to Skip to main content
15,564,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose here I have a structure coding like this:
C++
typedef int (*InputBkPaint)(HWND,pInputStyle,HDC,LPRECT);
typedef struct input_style {
    //...

    InputBkPaint paintbk;    
}InputStyle,*pInputStyle;

Noticed that the defination of a function pointer type InputBkPaint and following a structure defination which just has a member paintbk whos data type is a function pointer as above.
Since the defination of the function pointer has a parameter of data type pInputStyle , Now they are twisted.

If I just complier the code above , there would be a syntax error like undefined identifier.

What I have tried:

If I alter the function pointer defination like this:
C++
typedef int (*InputBkPaint)(HWND,void*,HDC,LPRECT);
, it would be OK with some data type conversion. But that's not what I want.

If I want remain those two typedef without any change, what should I do to make the complier work correctly ?
Posted
Updated 3-Sep-22 0:05am

C++
struct input_style;
typedef struct input_style* pInputStyle;
typedef int (*InputBkPaint)(HWND, pInputStyle, HDC, LPRECT);
typedef struct input_style {
    //...

    InputBkPaint paintbk;
}InputStyle;
 
Share this answer
 
Comments
Richard MacCutchan 3-Sep-22 6:05am    
ctest.c(11): error C2122: 'pInputStyle': prototype parameter in name list illegal
markkuk 3-Sep-22 6:09am    
You need to include the Windows headers that define HWND, HDC and LPRECT so that the compiler knows those are types, not parameter names. I used the Windows C++ application template in Visual Studio for testing.
Richard MacCutchan 3-Sep-22 6:22am    
:thumbsup: +5
Yount_0701 3-Sep-22 6:37am    
great! I believe your code is the right way which I ever saw in some open source code though scratching in my mind but forgot the exact coding details.
Some open source code or some interface provided by supplier may not want to expose the details of their implementations to the third part users, they do not open the structure members but just provide a method to access the members, there are many pre-declarations about the structure pointer in their open head files. They usually use your way! A good way.
Thank you @markkuk
I don't think you can. In order to get the typedef right, the size of the parameters / struct members have to be known in advance - and since they depend on each other that can't be the case. Even if all pointers are the same size, your function pointer typedef doesn't "know" that pInputStyle is a pointer until the second typedef is encountered.
And since the second typedef references the function pointer, it can;'t be declared first.

And C will not allow multiple typedefs for the same name (C++ will, but even then it's a dodgy thing to start doing).

void* is your only option, I fear.
 
Share this answer
 
Comments
Yount_0701 3-Sep-22 6:53am    
you're right. It maybe depends on compliers. MSVC works OK.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900