Indispensable Windows Native (8)-C language: struct, common body, enumeration, type definition

Source: Internet
Author: User

[SOURCE DOWNLOAD]


Indispensable Windows Native (8)-C language: struct, common body, enumeration, type definition



Webabcd


Introduced
Essential C language of Windows Native

    • Structural body
    • Shared body
    • Enumeration
    • Type-defined character



Example
CStruct.h

#ifndef _myhead_struct_ #define #ifdef __cplusplus   extern " C " #endif  Char *demo_cstruct (); #endif  


Cstruct.c

/** struct, common body, enumeration, type definition * Note: struct variables are passed in the argument, each member of the structure is transferred, which reduces the efficiency. So it is generally passed through a struct pointer*/#include"pch.h"#include"cStruct.h"#include"CHelper.h"voidstruct_demo1 ();voidStruct_demo2 ();voidStruct_demo3 ();voidStruct_demo4 ();voidUnion_demo ();voidenum_demo1 ();voidtypedef_demo1 ();//defines a struct type named birth/** struct Structure name * {* member table column *};*/structbirth{intYear//struct Members    intMonth//struct Members    intDay//struct Members};Char*demo_cstruct () {//Structure Foundation 1Struct_demo1 (); //Structure Foundation 2Struct_demo2 (); //struct pointer (pointer to struct variable)Struct_demo3 (); //struct ArrayStruct_demo4 (); //Common Body (Union)Union_demo (); //EnumerationEnum_demo1 (); //type-defined characterTypedef_demo1 (); return "look at the code and the comments.";}//Structure Foundation 1voidStruct_demo1 () {//define a struct type named employee    structEmployee {intNum//struct Members        Char*name ="Unknown";//struct member, you can set a default value for it        floatSarlary;//struct Members        structBirth birthday;//struct member, which can be another structure body    }; //declares a struct of type employee    structemployee Employee1; //assigns a value to a struct (accesses a struct member through the "." To achieve)Employee1.num = -; Employee1.name="WEBABCD"; Employee1.sarlary=100.5f; Employee1.birthday.year=1980; Employee1.birthday.month=2; Employee1.birthday.day= -; //occupies 24 bytes of memory space    intSize =sizeof(structemployee);}//Structure Foundation 2voidStruct_demo2 () {//commonly used struct types can be represented by macro definitions (also with TypeDef, later)#defineEmployee struct employeeEMPLOYEE {intnum; Char*name; } employee1, Employee2= { -,"WEBABCD"};//when declaring struct-body variables, you can initialize themEMPLOYEE employee3 = { -,"WEBABCD" }; //when you define a struct type, you can omit the struct name by declaring the variable .    struct    {        intnum; Char*name; } employee4, Employee5= { -,"WEBABCD"};//when declaring struct-body variables, you can initialize them//structs of the same type can be assigned to each other, and their members, whether they are basic types or pointers, will copy a newEmployee1 =Employee2; Employee2.num= About; Employee2.name="Wanglei"; //results: Employee1 (num=100, name= "WEBABCD"), Employee2 (num=99, name= "Wanglei")}//struct pointer (pointer to struct variable)voidStruct_demo3 () {structEmployee {intnum; Char*name;    }; structEmployee Employee1 = { -,"WEBABCD" }; //define a struct pointer (pointer to struct variable)    structEmployee *employee = &employee1; Char*name =Employee1.name; //(*employee)-is the structure content that the struct pointer points toName = (*employee). Name; //If you access the members of the struct that the struct pointer points to by using the struct pointer, you can implement theName = Employee->name;}//struct ArrayvoidStruct_demo4 () {structEmployee {intnum; Char*name;    }; //define a struct array    structEmployee employees[2] =    {        {  -,"WEBABCD" },        {  $,"Wanglei" }    }; //struct array and pointer relationships are the same as normal arrays and pointers    structEmployee *employee1 =employees; structEmployee *employee2 = &employees[0];}//Common Body (Union)voidUnion_demo () {//The so-called common body is to allow a number of variables to share a memory space, the common body occupies the space size of the common body of the largest basic type of members of the whole multiples, and the size to contain the common body of the largest type members//Note: The following are the results of my environment if any of the results are listed. For example, I have an int that takes 4 bytes.Union u//take 4 bytes (a takes up 4 bytes, B takes 4 bytes, because they share a memory space, so this common body consumes 4 bytes)    {        intA; intb;    } U1; u1.a=1; u1.b++; //The result of the above execution is that u1.a equals 2,u1.b equals 2 because they are sharing a memory spaceUnion//takes 12 bytes (a takes up 4 bytes, B takes 10 bytes, the common body has a maximum base type of int, it occupies 4 bytes, it is aligned, and the common body occupies 12 bytes)    {        intA//Common Body max basic type member        Chars[Ten];//Common body Maximum type member} U2; u2.a=0x61626364;//ABCD    CharS1 = u2.s[0];//D    CharS2 = u2.s[0];//C    CharS3 = u2.s[0];//b    CharS4 = u2.s[0];//a//by the visible, my CPU is the small end mode, that is, high address storage high byte, low address storage low byte//Note: Network byte order is in big-endian mode, that is, high-level data is stored in the low address (can be converted by Ntohs, Ntohl, htons, htonl)}//EnumerationvoidEnum_demo1 () {/** Enum Enum name * {* enumeration value table *}; */    //defines an enumeration type named weekday    enumWeekday {Sun, Mon, Tue, Wed, Thu, Fri, Sat} A, B, C= Fri;//declaring and initializing an enumeration variable//declares an enumeration variable of type weekday and initializesWeekday d = Sat;//enumeration value is not a string yo//If you are judging an enumeration value, you can use an enumeration value or an enumeration value to index    if(d = = Sat && D = =6)    {        //When you copy an enumeration value with an enumeration value index, you need to force the type conversionWeekday E = (enumWeekday3;//Wed    }        //When you define an enumeration type, you can omit the enumeration name if you declare the variable .    enum{v1, v2, v3} e1, E2, E3=v3;}//type-defined charactervoidTypedef_demo1 () {//typedef-type definition//The INTEGER type is the int typetypedefintINTEGER;    INTEGER A, B; //the INTP type is a pointer to type inttypedefint*INTP; //the NAME type is a char array of length 20typedefCharname[ -];    NAME A1, A2; //The STR type is the char pointertypedefChar*STR;    STR S1, S2; //The EMP type is the struct employee structtypedefstructEmployee {intnum; Char*name;    } EMP; EMP EMP1= { -,"WEBABCD" }; //Note: A macro definition is a string substitution, a typedef is a type substitution, a macro definition is done by preprocessing, and a typedef is done at compile time .}



Ok
[SOURCE DOWNLOAD]

Indispensable Windows Native (8)-C language: struct, common body, enumeration, type definition

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.