C structure syntax

Sample heading C.

typedef LPVOID UKWD_USB_DEVICE; typedef struct _UKWD_USB_DEVICE_INFO { DWORD dwCount; unsigned char Bus; unsigned char Address; unsigned long SessionId; USB_DEVICE_DESCRIPTOR Descriptor; } UKWD_USB_DEVICE_INFO, *PUKWD_USB_DEVICE_INFO, * LPUKWD_USB_DEVICE_INFO; 

My understanding

struct defines a structure (the part between {}). Structure Type _UKWD_USB_DEVICE_INFO . After closing } UKWD_USB_DEVICE_INFO is an alias of this structure .

Question

What is the purpose of the statements after that. * PUKD_USB_DEVICE_INFO and *LPUKWD_USB_DEVICE_INFO . Do these pointers aliases mean something else if one touches the variable and the other takes place between * and the label?

+5
source share
4 answers

C typedef declarations are understood by analogy with variable declarations.

 int a, *b; 

Declares the values โ€‹โ€‹of type a int and type b int* .

 typedef int A, *B; 

declares type a equivalent to int , and type b equivalent to int* . So, just think about what the type of the variable will be if it is a variable declaration.

So yes, PUKWD_USB_DEVICE_INFO becomes equivalent to struct _UKWD_USB_DEVICE_INFO* .

EDIT

In addition, space does not matter. C is a white space language. No additional aliases are needed; they just follow the conventions of various projects and APIs that like to call pointer types with names that include P or other substrings. Sometimes these projects end in many agreements over time, so there are several aliases. They may also be needed for compatibility reasons when upgrading the API or between different platforms.

+5
source

Yes, it is a pointer alias, you can use PUKWD_USB_DEVICE_INFO as UKWD_USB_DEVICE_INFO *. Most Windows structures do this:

enter image description here

The fact that L in the third alias stands for long (pointer), and if I am not mistaken, it does not make sense in 32-bit code of 64 bits - this will probably remain from 16 bits, as is the case with this WNDCLASS .

+1
source

Are these pointer aliases?

Yes.

Does this mean something if you are touching a variable and the other has a space between the * characters and the caption?

Not. In C, spaces between tokens do not matter to the compiler. They just change the readability for people who are looking at the code.

I have seen very few code examples on the Internet using more than one name after closing braces. Any understanding of this?

As a rule, and in this case, in particular, this is done in order to resolve symbol names, which may represent different types, but may also not .

You see that in your architecture the P "pointer and LP " long pointer "are of the same type.

In 16-bit architecture, you will look at a different header, and these types will be different.

+1
source

This definition style is common on the Windows platform. In the days of 16-bit segmented architectures, each typedef structure definition also had 2 typedefs for near and far (also called long pointers):

 typedef LPVOID UKWD_USB_DEVICE; typedef struct _UKWD_USB_DEVICE_INFO { DWORD dwCount; unsigned char Bus; unsigned char Address; unsigned long SessionId; USB_DEVICE_DESCRIPTOR Descriptor; } UKWD_USB_DEVICE_INFO, NEAR * PUKWD_USB_DEVICE_INFO, FAR * LPUKWD_USB_DEVICE_INFO; 

near pointers were 16 bits wide, and far pointers were 32 bits wide. Most Windows APIs took far pointers, and their prototypes used pointer typedefs. By the way, LPVOID was defined as follows:

 typedef void FAR *LPVOID; 

32-bit Windows appeared in 1995 and made it obsolete. near and far keywords were kept for a while, defined as empty, for compatibility reasons.

Compatibility with 16-bit Windows has long been useless, but usage is still delayed as typedef is still used, but the far and near keywords have been removed.

The space between * and PUKWD_USB_DEVICE_INFO ignored, but I agree with you that it is quite difficult to enter it there.

+1
source

All Articles