Indispensable Windows Native (8), windowsnative

Source: Internet
Author: User

Indispensable Windows Native (8), windowsnative

[Download source code]


Indispensable Windows Native (8)-C language: struct, shared body, enumeration, type specifier



Author: webabcd


Introduction
Indispensable C language for Windows Native

  • Struct
  • Shared body
  • Enumeration
  • Type specifier



Example
CStruct. h

#ifndef _MYHEAD_STRUCT_#define _MYHEAD_STRUCT_ #ifdef __cplusplus  extern "C"#endif  char *demo_cStruct();#endif  


CStruct. c

/** Struct, shared body, enumeration, and type specifiers ** Note: When passing parameters in struct variables, each member in the struct variables will be transferred, reducing efficiency. Therefore, it is generally passed through the struct pointer */# include "pch. h "# include" cStruct. h "# include" cHelper. h "void struct_demo1 (); void struct_demo2 (); void struct_demo3 (); void struct_demo4 (); void union_demo (); void enum_demo1 (); void typedef_demo1 (); // define a struct type named birth/** struct name * {* member table column *}; */struct birth {int year; // struct member int month; // struct member int day; // struct member}; char * demo_cStruct () {// struct base 1 struct_demo1 (); // struct base 2 stru Ct_demo2 (); // struct pointer (pointer to struct variable) struct_demo3 (); // struct array struct_demo4 (); // union_demo (); // enumerate enum_demo1 (); // type specifier typedef_demo1 (); return "read code and comment";} // struct base 1 void struct_demo1 () {// define a struct type struct employee {int num; // struct member char * name = "unknown"; // struct member, you can set the default value float sarlary for it; // struct member struct birth birthday; // struct Member, which can be another struct}; // declare a struct of the employee type struct Employee employee1; // assign a value to the struct.) employee1.num = 100; employee1.name = "webabcd"; employee1.sarlary = 100.5f; employee1.birthday. year = 1980; employee1.birthday. month = 2; employee1.birthday. day = 14; // The Memory occupied is 24 bytes int size = sizeof (struct employee);} // struct base 2 void struct_demo2 () {// common struct types can be expressed by macro definition (typedef can also be used later) # define EMPLOYEE struct employee {int num; char * nam E;} employee1, employee2 = {100, "webabcd"}; // when declaring a struct variable, You can initialize the EMPLOYEE employee3 = {100, "webabcd "}; // declare the variable when defining the struct type, you can save the struct name struct {int num; char * name;} employee4, employee5 = {100, "webabcd "}; // when declaring struct variables, You can initialize them. // struct of the same type can be assigned values to each other, both the basic type and pointer of the member copy the new employee1 = employee2; employee2.num = 99; employee2.name = "wanglei"; // result: employee1 (num = 100, name = "webabcd"), emp Loyee2 (num = 99, name = "wanglei")} // struct pointer (pointer to struct variable) void struct_demo3 () {struct employee {int num; char * name ;}; struct employee employee1 = {100, "webabcd"}; // defines a struct pointer (pointer to struct variable) struct employee * employee = & employee1; char * name = employee1.name; // (* employee)-it is the struct content pointed to by the struct pointer name = (* employee ). name; // if you use a struct pointer to access a member of the struct pointed to by the struct pointer, use "->" to implement name = employee-> name;} // struct array void struct_d Emo4 () {struct employee {int num; char * name ;}; // defines a struct array struct employee employees [2] ={{ 100, "webabcd "}, {200, "wanglei" }}; // relationship between struct arrays and pointers. The relationship is the same as that between General arrays and pointers. struct employee * employee1 = employees; struct employee * employee2 = & employees [0];} // shared body (Consortium) void union_demo () {// the so-called shared body means that several variables share one memory space, the size of the space occupied by the shared object is an integer multiple of the maximum basic type of the shared object, and the size must exactly include the maximum type of members of the shared body. // Note: if any of the following table lists the results, all are the results of my environment. For example, here int occupies 4 bytes of union u // occupies 4 bytes (a occupies 4 bytes and B occupies 4 bytes, because they share a single memory space, therefore, this shared body occupies 4 bytes) {int a; int B;} u1; u1.a = 1; u1. B ++; // the result of the above execution is that u1.a is equal to 2, u1. B is equal to 2 because they share a memory space. union // occupies 12 bytes (a occupies 4 bytes and B occupies 10 bytes. The maximum basic type of the shared body is int, it occupies 4 bytes and is aligned with it. The shared body occupies 12 bytes.) {int a; // char s [10], a member of the maximum basic type of the shared body. // Member of the largest shared body type} u2; u2.a = 0x61626364; // abcd char s1 = u2.s [0]; // d char s2 = u2.s [0]; // c char s3 = u2.s [0]; // B char s4 = u2.s [0]; // a // visible from top, my cpu is in small-end mode, that is, the high address stores the high byte, and the low address stores the low byte // Note: The network byte order is in the large-end mode, that is, the high data is stored in the low address (you can use ntohs, ntohl, htons, htonl conversion)} // enumeration void enum_demo1 () {/** enum enumeration name * {* enumeration value table *}; * /// defines an enum weekday {sun, mon, tue, wed, thu, fri, sat} a, B, c = fri; // declare and initialize the enumerated variable // declare a weekday-type enumerated variable and initialize weekday d = sat; // If the enumerated value is not a string, // you can determine whether an enumerated value is used, you can use an enumerated value or an enumerated value to index if (d = sat & d = 6) {// when an enumerated value index is used to copy the enumerated value, you need to forcibly convert the type to weekday e = (enum weekday) 3; // wed} // If you declare the variable when defining the enumeration type, you can save the enum {v1, v2, v3} e1, e2, e3 = v3;} // type specifier void typedef_demo1 () {// typedef-type specifier // The INTEGER type is int type typedef int INTEGER; INTEGER a, B; // the INTP type is the pointer typedef int * INTP to the int type; // The NAME type is the char array typedef char NAME [20]; NAME a1, a2; // The STR type is the char pointer typedef char * STR; STR s1, s2; // The EMP type is the struct employee struct typedef struct employee {int num; char * name;} EMP; EMP emp1 = {100, "webabcd"}; // note: macro definition is string replacement, typedef is type replacement, macro definition is completed by preprocessing, typedef is completed at compilation}



OK
[Download source code]

Related Article

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.