11. C language Structure and common body

Source: Internet
Author: User
Tags custom name

First, the structural body

struct: is a custom constructed data type

Purpose: To combine different types of data into a single whole.

struct type definition:

struct [struct name]

Type identifier member name;

Type identifier member name;

......

};(remember that semicolons are not limited)

eg

struct student

int num;

Char name[20];

char sex;

int age;

Float score;

Char addr[20];

}STU1,STR2; (STU1,STU2 is a struct variable)

Description

The structure type differs from the concept of structural body variables

Type: Do not allocate memory variable: allocate memory;

Type: Cannot assign value, access, operation variable: yes;

Members of struct variables can be used alone, such as ordinary variables;

structure can be nested;

      eg

struct date

{

int month;

int day;

int year;

};

struct student

{

int num;

Char name[20];

struct date birthday;

}stu;

Struct member names can be the same as variable names in a program, and they do not represent the same object.

    References to struct variables:

Reference rules

struct variables cannot be referenced collectively, only variable members can be referenced

struct variable name. Member name.

Stu.age = 20;

Correct usage of the struct:

1#include <stdio.h>2 voidMain ()3 {4     structStudent5     {6         intNo;7         floatscore;8         Charname[ -];9 }stu;Tenscanf"%d,%f,%s", &stu. no,&stu.score,&stu.name); Oneprintf"%d,%2.2f,%s", Stu. No,stu.score,stu.name); A}

How the struct is initialized:

(1) struct structure body name

{

Type identifier member name 1;

Type identifier member name 2;

};

struct struct body name struct variable ={initialization data};

(2) struct structure body name

{

Type identifier member name 1;

Type identifier member name 2;

} struct-Body variable ={initialization data};

   

struct array:

Structs with the same structure can also form arrays

Defining an array of structures: three forms

(1) Indirect definition

    struct student

{

int num;

Char name[20];

int age;

char sex;

};

   struct student stu[2];

Pointer to struct type data

Storage structure First Address

The operation of the structure is carried out according to the principle of the address arithmetic of the C language

For example, a struct pointer plus 1 will point to the next structure in memory

Pointer to struct variable

Definition form: struct structure body name * struct body pointer name;

struct student{    int num;    Char name[20];    char sex;    int age;} stu;struct Student *p = &stu;

The use of struct variable pointers is equivalent to the following three forms:

Stu.num = 101;

(*p). num=101;//brackets cannot be less

p->num=101;

Examples of usage:

1#include <stdio.h>2#include <string.h>3 voidMain ()4 {5     structStudent6     {7         Long intnum;8         Charname[ -];9         Charsex;Ten         floatscore; One}stu_1,*p; Ap=&stu_1; -Stu_1.num =89001; -strcpy (Stu_1.name,"Lilei"); the(*p). Sex ='M'; -P->score =89.5; -printf"\nno:%ld\nname:%s\nsex:%c\nscore:%f\n", (*p) .num,p->name,stu_1.sex,p->score); -}

Second, the common body

Shared Body Concept:

Constructs a data type, also called a union

Purpose: To make a number of different types of variables share a memory (mutual coverage)

Type definition form:

     Union Common body Name

{

Type identifier member name;

Type identifier member name;

};

eg

Union data

{

int i;

Char ch;

float F;

}a,b,*p,d[3]; (similar to struct)

Description: A community variable exists only one member at any time. Shared body variable definition allocates memory, length = number of bytes for the longest member

  How to refer to a shared body variable

    Three ways of equivalence:

Common body variable name. Member Name

Member name, shared body pointer name

(* shared body pointer name). Member Name

Features of the shared body type data:

    (1) The same memory segment can be used to store several different members, but in each moment, can only store one, instead of storing several

(2) The member that functions in the common body variable is the last member to be deposited

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

Third, enum type

  Enumeration type is added by the new ANSI C standard.

If a variable has only a few possible values, it can be defined as an enumeration type. The so-called enumeration refers to the value of the variable is enumerated, the value of the variable is limited to the list of values within the range.

Enumeration types and their variables are defined in the form:

    Enum Enum type name {enumeration element list} enumeration variable list;

    You can declare a type before you define a variable, or define a variable at the same time or directly.

eg

Enum weekday {Sun,mon,tue,wed,thu,fri,sat};

Enum weekday workday,week_end;

Or

Enum Weekday {Sun,mon,tue,wed,thu,fri,sat} workday,week_end;

Where Sun,mon......sat is an enumeration element or enumeration constant. They are user-defined identifiers.

Description

At compile time, the enumeration elements are treated as constants, they are not variables, and they cannot be assigned values.

Enumeration elements as constants, when they have values, are compiled in the order in which they are defined so that their values are 0,1,2 ...

Enumeration values can be used to make a judgment comparison:

eg

if (workday = = Mon) ...

if (Workday >sun) ...

An integer cannot be assigned directly to an enumeration variable, it should be forced to be cast before it can be assigned a value.

Eg:workday = (enum weekday) 2;

Iv. Defining types with typedef

Function: Name the existing data type with a custom name

Type definition simple form: typedef type name;

Type: Existing data type

Name: the alias defined for type

Description

1. typedef does not create new data types

2. typedef defines a type and cannot define a variable

3, typedef and define different

typedef definition Type Step:

1, according to the definition of variable method to write the definition of the body such as int i;

2, replace the variable name with the new type name int interger;

3, the front plus typedef such as typedef int Interger

4. Define variables such as Interger i,j with a new type name;

Type definitions can be nested

11. C language Structure and common body

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.