C language development tutorial _ structure, structure pointer, tyepdef, union, enum



1. Structure definition and initialization



// : A combination of a series of different types of data

// The array stores a series of collections of the same data type

// The structure is a collection that stores a series of different data types

struct Student // At this time No memory is allocated, but a structure variable is declared. The name of this variable is Student

{

    char name[20];

    int age;

    char gender[10];

    int classId;


};


struct Student2 

{

    char name[20];

    int age;

    char gender[10];

    int classId;


}Zeking;// The second way of definition


// The third way of definition

// The number of variables to lock the structure, and memory is allocated here

struct {

    char name[ 20];

    int age;

    char gender[10];

    int classId;


}stud3, stu4, stu5; // The variable name of the global anonymous structure of stud3, similar to the anonymous inner class in java, in order to lock the number of variables in the structure


struct People{

    char name[20];

    int age;

}Lucy = {"Lucy",90};


// Emphasis: Type! = variable. The structure name represents only the structure type, and there is no memory space.

// Members in the structure can be used alone

int main(){


    // The first way to define

    struct Student stu1; // At this time, memory is allocated, in the stack, local variables



    // ======= ===================================================== ===============


    struct People people1 = {"Zeking",20};

    struct People people2;

    // people2.name = "David";

    people2.age = 1;

    strcpy(people2.name, "lucy");

    people1.age = 10;


    printf("%s,%d \n", people1.name, people1.age);



    system("pause");

    return 0;

}

2. 结构体数组 结构体指针



struct People2{

    //char name[20];

    char *name;

    int age;

}Lucy2 = { "Lucy", 90 };


int main(){


    // 结构体数组 初始化

    int i;

    struct People2 stu[3] = { { "Zeking", 30 }, { "David", 32 }, { "Suci", 28 } };

    struct People2 s[5];

    for (i = 0; i < 5; i++){

        s[i].age = 20 + i;

        //strcpy(s[i].name,"Lucy");

        s[i].name = "lucy";

        // If it is char name[20], use strcpy to assign value

        // If you want s[i].name = "lucy", change the type of name to char* name

        // If you don't understand, you can go to the previous article blog


    }

    for (i = 0; i < 5; i++){

        printf("s %d:%s,%d\n",i,s[i].name,s[i].age);

    }


    // ============================= Structure pointer ================= ===========


    struct People2 *p = stu;// Assigning the first address of the array to p is equivalent to p = stu;


    struct People2 *p2;

    p2 = (People2 *)malloc(sizeof (struct People2) *4);

    // defines an array with 4 variables for People2, then assigns the address of this array to p2

    printf("%#x \n",&p2);

    memset( p2, 0, sizeof(struct People2) * 4);// Initialization, all are 0


    for (i = 0; i < 4; i++){

        //(p2 + i)->age = 20 + i; // p2+i involves the displacement of the pointer, the size of the displacement is sizeof(struct People2)*i

        //(p2 + i)->name = "zeking";

        // can also be written as

        p2[i].age = 20 + i;

        p2[i].name = "zeking";

    }

    for (i = 0; i < 4; i++){

        printf("p2 : %d:%s,%d\n", i, (p2 + i)->name, (p2 + i)->age);

    }



    system("pause");

    return 0;

}



3 . Add function pointer



struct Man{

    int age;

    char *name;

    int(*Msg)(char *,int);

};


int message(char *str, int age){

    MessageBox(0, TEXT("hello"), TEXT("Lijian"), 0);

    return 0;

}


int main(){


    struct Man man;

    man.age = 40;

    man.name = "Zeking" ;

    man.Msg = message;


    man.Msg(man.name,man.age);


    system("pause");

    return 0;

}

4. Add structure pointer member variable



struct Node {

    int data;

    Node * next;

};


//ArrayList

 

    list;

//Node node;

//list.add(node);


//Add a data at the end of the singly linked list

int enqueNode(Node *head, int data) {

    Node * node = ( Node *) malloc(sizeof(Node));

    if (node == NULL) {

        return 0;

    }

    node->data = data;

    node->next = NULL;


    //不让head的 本身值 改变

    Node *p = head;

    while(p->next != NULL) {

        p = p->next;

    }


    p->next = node;

    /*  while (head->next != NULL) {

        head++;

    }*/

    return 1;

}


int main() {

    int num = 10;

    int i = 0;

    Node * list;

    list = (Node *)malloc(sizeof(struct Node));

    list->data = 0;

    list->next = NULL;


    for (i = 0; i < num; i++) {

        enqueNode(list, i+1);

    }


    while (list->next != NULL)

    {

        printf("%d \n", list->data);

        list = list->next;

    }

    system( "pause");

    return 0;

}

  

5. The typedef directive



// ​​is just an alias

// java agent

// does not create a new data type, but creates an alias for an existing type


typedef int _in;

typedef char * string;


typedef int(*PFI)(char *, char *);


typedef Tnode * Treeptr;

typedef struct Tnode {

    char *word;

    int count;


    /*Tnode * left;

    Tnode * right;*/

    Treeptr left;

    Treeptr right;

} BinaryTreeNode;


int fun(char *, char *) {

    return 0;

}

int main() {

    _in a = 20;

    printf("%d\n", a);


    string str;

    str = "hello world" ;


    PFI fp;

    fp = fun;


    char * ch;

    ch = "hello world";


    BinaryTreeNode* node;

    node = (BinaryTreeNode *) malloc(sizeof(BinaryTreeNode));


    system("pause");

    return 0;

}

6. Common body, enumeration



//union

//Put data of different data types into the same segment of memory.

//The occupied memory size is the largest memory size in the data type

union MyUnion

{

    int a;

    char b;

    float c;

};


int main() {

    MyUnion unio;


    unio.a = 10;

    unio.b = 'a';

    unio.c = 1.2f;

    printf("a: %#x, b: %#x, c: %#x\n", &unio.a, &unio.b, &unio.c);

    // only the most recently assigned variable

    printf("a: %d, b: %c , c: %f\n", unio.a, unio.b, unio.c);


    system("pause");

    return 0;

}


enum {

    monday = 10,

            saturday, , ,

    sunday,

};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326418446&siteId=291194637