Struct, struct Member

Source: Internet
Author: User

Struct, struct Member

In the C language world, a program is composed of a struct plus algorithm. Therefore, the struct type is indispensable in the interaction with Win32 APIs. Classes defined in Python cannot be directly used in C functions. They must be inherited by the structure classes in ctypes and filled in fields according to their format. The following example defines the simplest struct that represents the coordinate struct of a point on the screen, as shown below:

# Windows application # developer: Cai junsheng (QQ: 9073204) 2014-8-14from ctypes import * class POINT (Structure): _ fields _ = [("x", c_int ), ("y", c_int)] # point = POINT (10, 20) print (point. x, point. y) point = POINT (y = 5) print (point. x, point. y)

In this example, a class POINT is defined to inherit the Structure class in the ctypes library, and the member list _ fields _ in the class POINT is assigned a value, each struct member is represented by a tuples of two elements ("x", c_int )). The first field of the tuples indicates the name of the field, and the second field indicates the type of the field. In this way, we can describe the struct defined in C language. You can also use the defined struct In the struct, as shown in the following example:

class FILETIME(ctypes.Structure):    _fields_ = [("dwLowDateTime", DWORD),                ("dwHighDateTime", DWORD)]...class WIN32_FIND_DATAA(ctypes.Structure):    _fields_ = [("dwFileAttributes", DWORD),                ("ftCreationTime", FILETIME),                ("ftLastAccessTime", FILETIME),                ("ftLastWriteTime", FILETIME),                ("nFileSizeHigh", DWORD),                ("nFileSizeLow", DWORD),                ("dwReserved0", DWORD),                ("dwReserved1", DWORD),                ("cFileName", CHAR * MAX_PATH),                ("cAlternateFileName", CHAR * 14)]

In the WIN32_FIND_DATAA struct, the custom struct FILETIME is referenced as the type declaration. Define the struct for data exchange with the C interface in Python. The most special thing is to inherit the class ctypes. Structure. Otherwise, you must remember this point.



C language struct Definition

The second struct student defines a student struct.

The first is to use typedef to redefine the struct type name of struct student as student. That is to say, struct student and student indicate that the same thing is a type identifier, such as typedef int zhengshu; that is, you rename the integer int to zhengshu. The following definitions: int I; and zhengshu I; are equivalent.

C language struct Definition

The second struct student defines a student struct.

The first is to use typedef to redefine the struct type name of struct student as student. That is to say, struct student and student indicate that the same thing is a type identifier, such as typedef int zhengshu; that is, you rename the integer int to zhengshu. The following definitions: int I; and zhengshu I; are equivalent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.