IOS Development Series-construction type of c language, ios development-c Construction

Source: Internet
Author: User

IOS Development Series-construction type of c language, ios development-c Construction
Overview

In the first section, we mentioned the construction type of C language, which can be divided into arrays, struct, enumeration, and shared bodies. Of course, the content of the preceding arrays has been mentioned a lot, this section focuses on the other three types.

Struct

The array stores a series of the same data types. If you want a variable to store different data types, you must use struct, struct definitions are similar to class definitions in C ++, C #, Java, and other advanced languages, but in fact they are quite different. Struct is a type, not a variable, but it can be composed of other basic types of C language.

//// Main. c // ConstructedType /// Created by Kenshin Cui on 14-7-18. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # include <stdio. h> // struct type Datestruct Date {int year; int month; int day ;}; struct Person {char * name; int age; struct Date birthday; // a struct uses another struct type. The struct keyword float height must be added before the struct type variable Declaration;}; int main (int argc, const char * argv []) {struct Person p = {"Kenshin", 28, {1986,8, 8}, 1.72}; // you cannot define and initialize struct variables before initialization. For example: struct Person p; p = {"Kenshin", 28, {1986,8, 8}, 1.72}; incorrect printf ("name = % s, age = % d, birthday = % d-% d, height = %. 2f \ n ", p. name, p. age, p. birthday. year, p. birthday. month, p. birthday. day, p. height); // result: name = Kenshin, age = 28, birthday = 1986-8-8, height = 1.72, the reference of the struct is through "struct variable. member name "printf (" len (Date) = % lu, len (Person) = % lu \ n ", sizeof (struct Date), sizeof (struct Person )); // result: len (Date) = 12, len (Person) = 32 return 0 ;}

The preceding example must be described as follows:

For example, the preceding code runs in a 64-bit Compiler (int length 4, char length 1, float type 4 ), date = 4 + 4 + 4 = 12. But it is not that simple for Person, because the Person = 8 + 4 + 12 + 4 = 28 is calculated in the normal way, but the result given in the above Code is 32. Why? Here we have to introduce a concept of "memory alignment". The concept of memory alignment is not described in detail here. What you need to know is: in Mac OS X, the default alignment parameter is 8 (you can add # pragma pack (8) in the code to change the alignment parameter). If the type of the struct is not greater than 8, the length of a struct is the sum of its member types. However, if the length of a member variable is greater than that of this alignment parameter, the result is not necessarily the sum of member variables. The reason why the length of the Person type is 32 is that the offset 12 of the Date type is not a multiple of 8 when it is stored. To solve the memory alignment, you need to add four completion lengths, the following table lists the specific causes:

For details about the table source, watch the following video (note that the recording software does not affect the analysis after several seconds ):

Next, let's take a look at the struct array and pointer to the struct:

//// Main. c // ConstructedType /// Created by Kenshin Cui on 14-7-18. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # include <stdio. h> struct Date {int year; int month; int day;}; struct Person {char * name; int age; struct Date birthday; float height ;}; void changeValue (struct Person person) {person. height = 1.80;} int main (int argc, const char * argv []) {struct Person persons [] = {"Kenshin", 28, {1986,8, 8 }, 1.72 },{ "Kaoru", 27, {1987,8, 8}, 1.60 },{ "Rosa", 29, {1985,8, 8}, 1.60 }}; for (int I = 0; I <3; ++ I) {printf ("name = % s, age = % d, birthday = % d-% d, height = %. 2f \ n ", persons [I]. name, persons [I]. age, persons [I]. birthday. year, persons [I]. birthday. month, persons [I]. birthday. day, persons [I]. height);}/* output result: name = Kenshin, age = 28, birthday = 1986-8-8, height = 1.72 name = Kaoru, age = 27, birthday = 1987-8-8, height = 1.60 name = Rosa, age = 29, birthday = 1985-8-8, height = 1.60 */struct Person person = persons [0]; changeValue (person ); printf ("name = % s, age = % d, birthday = % d-% d, height = %. 2f \ n ", persons [0]. name, persons [0]. age, persons [0]. birthday. year, persons [0]. birthday. month, persons [0]. birthday. day, persons [0]. height);/* output result: name = Kenshin, age = 28, birthday = 1986-8-8, height = 1.72 */struct Person * p = & person; printf ("name = % s, age = % d, birthday = % d-% d, height = %. 2f \ n ", (* p ). name, (* p ). age, (* p ). birthday. year, (* p ). birthday. month, (* p ). birthday. day, (* p ). height);/* output result: name = Kenshin, age = 28, birthday = 1986-8-8, height = 1.72 */printf ("name = % s, age = % d, birthday = % d-% d, height = %. 2f \ n ", p-> name, p-> age, p-> birthday. year, p-> birthday. month, p-> birthday. day, p-> height);/* output result: name = Kenshin, age = 28, birthday = 1986-8-8, height = 1.72 */return 0 ;}

As a function parameter, struct transmits the value of a member (value transfer rather than reference transfer). For struct pointers, they can be accessed through the "->" operator.

Enumeration

The enumeration type is a simple data type. In fact, the enumeration type in C language is processed as an integer constant, which is usually called an enumeration constant ".

//// Main. c // ConstructedType /// Created by Kenshin Cui on 14-7-18. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # include <stdio. h> enum Season {// by default, spring = 0, summer = 1, autumn = 2, winter = 3 spring, summer, autumn, winter}; int main (int argc, const char * argv []) {enum Season season = summer; // enumeration value assignment, equivalent to season = 1 printf ("summer = % d \ n", season ); // result: summer = 1 for (season = spring; season <= winter; ++ season) {printf ("element value = % d \ n", season );} /* result: element value = 0 element value = 1 element value = 2 element value = 3 */return 0 ;}

Note that the default value of enumeration members starts from 0. If one of the members is assigned a value, the other later members are assigned a value in sequence. For example, if the summer is manually set to 8, autumn = 9, winter = 10, while sprint is still 0.

Shared body

A shared object is also called a union because its keyword is union (which seems to be frequently used in Database Operations). It is not used as frequently as enumeration and struct, however, as a data type in C language, we also need to understand its usage. From the previous analysis, we know that the total length of the struct is equal to the sum of all the members (of course, alignment problems may also occur). However, unlike the struct, all the members share a piece of memory, the sequence is stored from the lower address. Only one member can be used at a time. The final size of union is determined by the largest member in the shared body. assigning values to a Member may overwrite the other member.

//// Main. c // ConstructedType /// Created by Kenshin Cui on 14-7-20. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # include <stdio. h> union Type {char a; short int B; int c ;}; int main (int argc, const char * argv []) {union Type t; t. a = 'a'; t. B = 10; t. c = 65796; printf ("address (Type) = % x, address (t. a) = % x, address (t. b) = % x, address (t. c) = % x \ n ", & t, & t. a, & t. b, & t. c); // result: address (Type) = 5fbff7b8, address (t. a) = 5fbff7b8, address (t. b) = 5fbff7b8, address (t. c) = 5fbff7b8 printf ("len (Type) = % d \ n", sizeof (union Type); // result: len (Type) = 4 printf ("t. a = % d, t. B = % d, t. c = % d \ n ", t. a, t. b, t. c); // result: t. a = 4, t. B = 260, t. c = 65796 return 0 ;}

 

Here we need to explain the question: why t. a, t. b. t. c output results are 4, 260, and 65796 respectively, of course t. it's not surprising that c equals 65796, but t. shouldn't the value 'a' be 97, but t. shouldn't B be 10? In fact, if we find out the concept of sharing the body, we will be clear.

As mentioned above, the shared body can only use one of the members at a time. After three assignments to the above Code, t is used. c, and through the output above, we do see that c is valid. A feature of a shared object is that its members are stored in the same memory area. The size of this area depends on the maximum member length of its members. Since the above Code is compiled in a 64-bit compiler, the specific length: char = 1, short int = 2, int = 4, so it is concluded that the length of Type is 4, based on the output address, you can obtain the following storage Information (note the data storage method: High-address storage, low-address storage status ):

When reading c, its binary value is "00000000 00000001 00000001 00000100", which is converted to decimal value 65796. After three assignments, at this time, B's storage has been overwritten by low data of c members, and B's length is two, so the binary data obtained from the starting address is "00000001 00000100" (B's original data has been overwritten by c's low 2-bit data, in fact, this is the low 2-bit data of c), and the conversion to decimal is 260; similar to a, the data at this time is the low 1-bit data of c "00000100", and the conversion to decimal is 4.


Q: What types of constructor are in C language?

Array struct shared body

C Language Structure Type

Typedef struct Node {
Int data;
Struct Node * next;
} LNode;
Node is the name of a struct. struct Node ff is required for definition. ff is a variable.
LNode is another name of Node because you use "typedef" and you can directly use LNode ff to define the variable of this struct.
This is another name for the struct Node.

Typedef struct {
Int data;
Struct Node * next;
} LNode;
This statement directly names this struct as LNode,
That is to say, there is no struct Node ff; this definition method can only be defined using LNode ff.
The reason is because typedef. Without this keyword, LNode is only a variable of this struct and cannot be defined. That is to say, only one variable of this struct is ---- LNode.
For example: LNode ff; this method is incorrect.
Typedef struct {
Int data;
Struct * next;
} LNode; this is not feasible. At least I can see it. The error is caused by a problem with struct * next.

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.