C Language Development tutorial _ struct, struct pointer, tyepdef, union, enum, tyepdefenum

Source: Internet
Author: User

C Language Development tutorial _ struct, struct pointer, tyepdef, union, enum, tyepdefenum
1. struct definition and initialization

//: The combination of a series of different types of data // array stores a series of sets of the same data type // struct stores a series of sets of different data types struct Student // memory is not allocated at this time, only a struct variable is declared. The variable name 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 definition method // The third definition method // The number of variables that lock the struct. The memory struct {char name [20]; int age is allocated here; char gender [10]; int classId;} stud3, stu4, stu5; // name of the global anonymous struct in stud3 , Similar to the anonymous internal class in java, In order to lock the number of struct variables struct People {char name [20]; int age ;}lucy = {"Lucy", 90 }; // emphasize: type! = Variable. The struct name represents only the struct type and has no memory space. // The members in the struct can use the int main () {// struct Student stu1 defined in the first way; // memory is allocated at this time, in the stack, local variable // ================================================= ========================================================== 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 array struct pointer
Struct lele2 {// char name [20]; char * name; int age;} Lucy2 = {"Lucy", 90}; int main () {// struct array initialize 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"; // assign a value to strcpy for char name [20] // If s [I] is required. name = "lucy" should change the name type to char * name // if you do not understand it, go to the previous blog} for (I = 0; I <5; I ++) {printf ("s % d: % s, % d \ n", I, s [I]. name, s [I]. age );} // ============================== struct pointer ========== ============================= struct lele2 * p = stu; // assign the first address of the array to p, which is equivalent to p = stu; struct People2 * p2; p2 = (People2 *) malloc (sizeof (struct People2) * 4 ); // defines an array with four People2 variables, and then assigns the address of this array to p2 printf ("% # x \ n", & p2 ); memset (p2, 0, sizeof (struct People2) * 4); // initialization, all for 0 for (I = 0; I <4; I ++) {// (p2 + I)-> age = 20 + I; // p2 + I involves pointer displacement. The displacement is sizeof (struct People2) * I // (p2 + I)-> name = "zeking"; // You can also write p2 [I] below. 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 a function pointer to the struct.
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 the struct pointer member variable to the struct.
Struct Node {int data; Node * next;}; // ArrayList
 
  
List; // Node node; // list. add (node); // add a data int enqueNode (Node * head, int data) {Node * node = (Node *) to the end of a single-chain table *) malloc (sizeof (Node); if (node = NULL) {return 0;} node-> data = data; node-> next = NULL; // do not let the head value change 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. typedef command
// Is the alias // java proxy // no new data type is created, but the alias typedef int _ in is created for the existing type; 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. public body, enumeration
// Union // put different data types into the same memory. // The memory size occupied is the maximum memory size occupied by 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 recently assigned variables printf ("a: % d, B: % c, c: % f \ n", unio. a, unio. b, unio. c); system ("pause"); return 0;} enum {Monday = 10, Saturday, Sunday ,};

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.