Crazy C language Notes (struct/shared/enumeration)

Source: Internet
Author: User
Tags define null



(i) Structure type

1. Introduction:

Cases:

struct date

{

int month;

int day;

int year;

};

struct student

{

int num;

Char name[20];

char sex;

int age;

struct date birthday; /*birthday is a struct date type */

Char addr[30];

}student1,student2;

(1): structure can be nested

(2): structure body elements are variables compared to enumerations, and enumerations are constants

(3): The element can only be a single reference: such as: Student.num; Student.birthday.month;

(4): Internal element variables can be identical to other variables and do not affect each other;

(5): assignment: Student.num = 10010;

2. Initialization

(Can be directly assigned value)

struct student

{

long int num;

Char name[20];

char sex;

Char addr[20];

}a = {10101, "Li Lin", "M", "Beijing Road"};

3. Structure array

struct student

{

int num;

Char name[20];

char sex;

int age;

struct date birthday; /*birthday is a struct date type */

Char addr[30];

};

struct student stu[3];

Note: Each element in the array is a struct type data;

4. Pointers to struct type data

struct student

{

int num;

Char name[20];

char sex;

int age;

struct date birthday; /*birthday is a struct date type */

Char addr[30];

};

struct student stu_1;

struct student *p;

p = &stu_1;

Note:

(1) A pointer to a struct variable is the starting address (the beginning address) of the memory segment occupied by the variable;

(2) (*p). Num is equivalent to stu_1.num; (brackets at both ends of *p cannot be omitted)

(3) (*p). Num is equivalent to p->num; (--Refer to operator)

(4) The following operations

P->n; gets the value of member n in the struct variable that p points to.

P->n + +; Gets the value of member n in the struct variable that the P points to, and uses that value to add 1.

+ + p->n; Gets the value of member n in the struct variable that the P points to, plus 1, and then uses it.

Operation priority is greater than + +;

5. Pointers to struct array elements

struct student

{

int num;

Char name[20];

char sex;

int age;

struct date birthday; /*birthday is a struct date type */

Char addr[30];

};

struct student stu[3];

struct student *p;

for (p = stu;p < Stu + 3;p + +)

printf ("%5d%-20s%2c%4d\n", p->num,p->name,p->sex,p->age);

Note: P is a pointer variable pointing to the struct student type data, which is used to point to the data of a struct student type (and the starting address of an element of the Stu array, such as stu[0],stu[1], which should not be used to point to a member of the STU array element ,

p = stu[1].name; This usage is wrong.

But:

Forced type conversion can be used after

p = (struct student *) Stu[0].name; This is the right thing to do;

In this case: the value of P is the starting address of the name member of the stu[0] element. printf ("%s", p) outputs the value of the member name in Stu[0]. But printf ("%s", p + 1) outputs the value of name in Stu[1]. The value of p+1 is the byte length of the struct struct student, based on the P-value.

because: P plus minus operation his type is his smallest unit of operation.
  

6. Structure variables do function parameters

Note: When using structure variables to function parameters, the contents of the memory units accounted for by the structure variables are passed to the formal parameters in the way of value transfer.

#include <stdio.h>

#include <string.h>

#define FORMAT "%d\n%s\n%f\n%f\n%d\n"

struct student

{

int num;

Char name[20];

float score[3];

} ;

void Main ()

{

void print (struct student);

struct student Stu;

Stu.num = 12345;

strcpy (stu.name, "Li Li");

Stu.score[0] = 67.5;

STU.SCORE[1] = 89;

STU.SCORE[2] = 78.6;

Print (STU);

}

void print (struct student stu)

{

printf (format,stu.num,stu.name,stu.score[0], stu.score[1],stu.score[2]);

printf ("\ n");

}

Operation Result:

12345

Li Li

67.500000

89.000000

78.599998

7. Working with pointers for linked lists

Cases:

#include <stdio.h>

#define NULL 0

struct student

{

Long num;

Float score;

struct student *next;

};

void Main ()

{

struct student a,b,c,*head,*p;

A.num = 10101;a.score = 89.5;

C.num = 10103;c.score = 90;

C.num = 10107;c.score = 85;

Head = &a;

A.next = &b;

B.next = &c;

C.next = NULL;

p = head;

Do

{

printf ("%ld%5.1f\n", p-and num,p-score);

p = p and next; Make P point to the next node

}

while (P! = NULL);

}

Operation Result:

10101 89.5

10103 90.0

10107 85.0

Note:

This example node is defined in the program, is not a temporary open-up, and can not be used up after release, this list is called "Static linked list."

8. Working with dynamic linked lists

①malloc function

Prototype: void *malloc (unsigned int size)

The function is to allocate a continuous space of size in the dynamic storage area of the memory. The value of this function (that is, "return value") is the starting address of an assigned field (Type void). If this function fails to execute successfully (for example, out of memory), a null pointer (NULL) is returned.

②calloc function

Prototype: void *malloc (unsigned n unsigned int size);

Its role is to allocate n contiguous spaces of size in the dynamic storage of memory. The function returns a pointer to the starting position of the assigned domain, or null if the assignment is unsuccessful;

③free function

Prototype: void free (void *p);

The purpose of this is to release the dynamic storage that is pointed to by P, so that this part of the memory area can be used by other variables. P is the value returned when the Calloc or malloc function was last called. The free function has no return value.

9. Create a dynamic link list

(ii) Shared body

1. Overview: make several different variables share the same piece of memory structure, called "Common body" type of structure;

2. Definition

Union Common body Name

{

Member table column;

} variable table column;

Such as:

Union data

{

int i;

Char ch;

float F;

}a,b,c;

Or

Union data

{

int i;

Char ch;

float F;

};

Union Data a,b,c;

Note: The common body is similar to the structure definition form, but the memory is different, the structure is the memory of each member and the memory of the common body is the longest byte in the member;

3. How to refer to a shared body variable

You must first define a post-reference, and reference only members in a pooled variable.

Such as:

A.i

a.ch

A.f

4. Characteristics of shared body type data

(1) The same memory segment can be used to hold several different types of members, but only one at a time, instead of storing several . Therefore, the member that functions in the shared body variable is the last member to be deposited.

Such as:

A.I = 1;

A.C = ' a ';

A.F = 1.5;

Only A.F is valid, A.I, A.C is meaningless, at this time with "printf ("%d ", a);" No, "printf ("%d ", A.I);" No, "printf ("%d ", A.F);" Can ;

(2) The address of the shared body variable and the address of its members are the same address.

namely : &a, &a.i, &A.C, &A.F are the same address ;

(3) You cannot assign a value to a shared body variable name , You cannot assign a value to a shared body name , and you cannot define a common body when you initialize it .

Union data

{

int i;

Char ch;

float F;

}a={1, ' A ', 1.5}; Error

Ii

A = 1; error

M =a; error

(4) A shared body variable cannot be used as a function parameter, nor can it be brought back to a shared body variable, but a pointer to a shared body variable (similar to the use of a struct variable) may be used.

(5) A common body type can appear in a struct type definition, or you can define a common body array. Conversely, structs can also appear in the definition of a common body type, and arrays can also be members of a common body.

(iii) enumeration type

1.enum Weekday{sum,mon,tue,wed,thu,fri,sat}; (the amount of enumeration definition is constant)

Declares an enum-type enum weekday, which can be used to define a variable. Such as:

Enum weekday workday,week_end;

Workday and week_end are defined as enumeration variables, and their values can only be sum to one of the SAT . Such as:

Workday = Mon;

Week_end = sum;

Equivalent to

enum {Sum,mon,tue,wed,thu,fri,sat} workday,week_end;

They are one of them.

2. Of course you can use

typedef enum {SUM,MON,TUE,WED,THU,FRI,SAT} weekday;

That is equivalent to enum WEEKDAY{SUM,MON,TUE,WED,THU,FRI,SAT};

3. Enumeration type characteristics

(1) Enumeration elements are constants, they are valued, and C languages are compiled in the order in which they are defined so that their values are 0,1,2 ....

In the definition above , Sun has a value of 0,mon of 6 for a value of 1......sat.

For example:

printf ("%d", workday);

The integer 1 will be output.

Ii

You can assign an initial value to an element in a definition

Such as:

Enum Weekday {sum =7,mon =1,tue,wed,thu,fri,sat} workday,week_end;

Define Sun as 7,mon to 1, and other values in order, that is, the sat is 6.

③ enumeration class values can be judged by comparison of size.

(2) An integer cannot be assigned directly to an enumeration variable

Such as:

Workday = 2; error

But can

workday = (EUNM weekday) 2; correct

Crazy C language Notes (struct/shared/enumeration)

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.