Dark Horse Programmer---C language foundation---structure

Source: Internet
Author: User

------iOS training, Java training, Android training, iOS learning technology blog, looking forward to communicating with you------

C Language Foundation---structure

I. What is a structural body

Before I introduce the structure, I would like to briefly introduce the following arrays. I think we're all familiar with the array. As the name implies, arrays are a combination of some data (elements) as a whole. The use of arrays requires attention that these elements must be of the same type. Structs and arrays are similar and combine some data together as a whole, but these elements can be of different types. It can be understood that structs are more flexible arrays because they allow elements to be of different types.

In real life, if we want to express a more complex whole, in Java, we can define a class, because Java features are object-oriented and then define various properties. But C language is a process-oriented language, how to express a more complex whole? ---is the use of structures.

Ii. definition of struct type:

Because the struct is not the data type that comes with the system, it cannot be brought by the system, because the structure is defined by the programmer according to the need. Therefore, before you define struct-body variables, you first need to customize the corresponding struct type.

The definition form of a struct type:

struct struct type name {

Type name 1 member name 1;

Type Name 2 member name 2;

Type Name 3 member name 3;

...

};

Where the struct is the keyword.

Cases:

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

};

The code above defines a struct called book ( Note: the definition structure can be understood to tell the system which members of the structure, just play the role of telling the system, not allocating memory space at this time ), a total of 3 members, namely: Name, author, price. This is somewhat similar to Java's object-oriented.

Third, the definition of structural variables:

The code above simply defines the struct type, because the struct type is not the system itself, and it cannot be defined by the system, unlike the basic data types such as int, float, double, char, which can be defined by the system. The struct type can only be defined by the programmer according to the specific requirements, so the definition of the struct type only tells the system which members are included in the structure of its own definition, but does not allocate memory space as a function of description. If you want to use the struct, you must define the struct variables for the template according to the custom struct type , at which time the system allocates memory to the struct variables.

Structure variable definition form:

1> define struct type first, then define struct body variable

defining struct types

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

};

struct book book;//define struct variable, variable named book.

2> defining struct-body variables while defining struct-type

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

} book; struct variable named book

3> directly define struct type and variable, omit struct type name

struct {

Char *name; Title

Char *author; The author of the book

float price; The price of the book

} book; struct variable named book

Note: The system does not allocate memory space when defining struct type, and the system allocates memory space when defining struct variables.

Iv. note points for structural type definitions

The 1> struct type itself cannot be defined recursively, that is, a member of a struct type is not allowed to be the struct type itself

Wrong wording

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

struct book book;//This line is the wrong wording

};

2> struct types can contain other struct types inside the body type

For example, define a book's structure type, its members include the title, author, selling price, publication date, but the publication date can also be used as a struct type whose members include the year, month, and day. The case is that the struct contains other structures.

struct pubdate{

int year;

int month;

int day;

}; Defines the date structure type, where no memory space is allocated at this time

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

struct PubDate PubDate;//Publication date of the book, structure Body contains another structure

}; Defines the structure type of the book and does not allocate memory space at this time

struct book book; Defines a struct variable named book, at which time the system allocates memory space to the book variable.

The memory space occupied by a struct variable is the sum of the memory space occupied by its members, and the members are arranged in memory in the order in which they are defined.

Therefore, the above variable book in the 64-bit compiler environment, the total memory is: 8+8+4+4+4+4=32 bytes According to the algorithm, may be 36 bytes

V. Initialization of structures

The initial values of each member of the struct are placed in the {} in the order in which they are defined, separated by commas

Cases:

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

};

struct book book={"iOS Development Guide", "CF", 30.5};//define the struct variable and initialize it, and the variable name is book.

Note: Initialization assignments can only be performed while defining struct-body variables.

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

};

Define struct-body variables and initialization assignments separately, error

struct book book;

Book = {"iOS Development Guide", "CF", 30.5};//Error

We can think about it and connect it to the array. When defining an array, the array name cannot be used to initialize the assignment, because the array name is the address of the first element of the array, the address of the array, and is a constant. Since the array name is constant, how can you initialize the assignment? Similarly, the struct variable name is not good, so the above notation is wrong.

Vi. How to use a custom structure

The operation of a struct variable is in the form of a member, in the following way: struct variable name. Member Name

1> with DOT syntax

“.” is the member operator, with the highest precedence in the operator

Cases:

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

};

struct book book;

Access the price member of book and assign a value

book.price=55.5;

The member operator "." Can be used consecutively in cases where other structures are included inside the struct.

Cases:

struct pubdate{

int year;

int month;

int day;

}; Defines the date structure type, where no memory space is allocated at this time

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

struct PubDate PubDate;//Publication date of the book, structure Body contains another structure

}; Defines the structure type of the book and does not allocate memory space at this time

struct book book; Defines a struct variable named book, at which time the system allocates memory space to the book variable.

book.pubdate.year=2015;

book.pubdate.month=1;

Book.pubdate.day=1;

And so on, you can theoretically use countless "." Access to all members.

VII. structure Array

What is a struct array? As the name implies, each element of an array is a struct type

1. How the structure array is defined:

Define struct array variable, variable named book, number of elements is 3

1> struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

};

struct book book[3];

2> struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

} Book[3];

3> struct {

Char *name; Title

Char *author; The author of the book

float price; The price of the book

} Book[3];

2. Initialization of an array of structures, for example:

struct book{

Char *name; Title

Char *author; The author of the book

float price; The price of the book

} book[3]={{"Dark Horse iOS Essentials Tutorial", "CF", 55.5},{"Dark Horse iOS Advanced Tutorial", "Peak", 35.5},{"Dark Horse iOS QuickStart", "CF", 35.5};

Access to an element of an array (a struct) using the array's access method (using an array subscript), and then using the member operator "." The member that accesses the element (struct).

Eight, pointer to the structure body

Learning to be comprehend by analogy, know the C language pointer, also know the structure, then understand the pointer to the structure of the body is not a difficult thing.

We know that each variable has its own memory address, then the struct variable is no exception, since it has its own memory address, then you can assign the address to the pointer variable, so the pointer can point to the struct variable.

A pointer variable that points to a struct is defined in the form:

struct struct type name * pointer variable name;

Then there are three ways to access struct members:

1> struct variable name. member name;

2> (* pointer variable name). member name;

3> pointer variable name, member name;

Cases:

struct book{

Char *name; Title

Char *author; Author

float price; Price

} book={"Dark Horse iOS Basic Tutorial", "CF", 55.5};

struct book *p;

p=&book;

Three ways to access struct members

printf ("name=%s,author=%s,price=%f\n", Book.name, Book.author, Book,price);

printf ("name=%s,author=%s,price=%f\n", (*p). Name, (*p). Author, (*p). Price);

printf ("name=%s,author=%s,price=%f\n", P->name, P->author, P->price);

The output is:

Name= Black Horse Basic tutorial, author=cf,price=55.500000

Name= Black Horse Basic tutorial, author=cf,price=55.500000

Name= Black Horse Basic tutorial, author=cf,price=55.500000

Dark Horse Programmer---C language foundation---structure

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.