IOS development completes C language every minute-struct and enumeration

Source: Internet
Author: User

IOS development completes C language every minute-struct and enumeration

I. struct
In the previous array sharing, we saved data of the same type through an array. We can use an array to represent this whole, but the array has the following features: each element must be of the same type. How can we store different types of data in one way?
Therefore, the C language provides a specially constructed type to solve the above problem. This is the struct, which allows internal elements to be of different types.

Ii. struct Definition
1. First define the struct type, in the definition of struct Variables
Struct student {
Int age;
Char * name;
Double height;
};
Struct student sd;

2. Define struct types while defining struct Variables
Struct student2 {
Int age;
Char * name;
Double height;
} Sd2;
Sd2.age = 16;
Sd2.name = "JAMES ";
Sd2.height = 1.9;

3. Define struct types while defining struct variables, and omit struct names
Struct {
Int age;
Char * name;
Double height;
} Sd3;
Sd3.age = 17;
Sd3.name = "james ";
Sd3.height = 1.88;

When defining the struct type, you must also define the struct variable and omit the struct name. Note:
Disadvantage: the struct type cannot be used in the future because it has no name.
Advantage: If the struct type only needs to be used once, you can use this method

Iii. Structure Initialization
1. Definition initialization at the same time
Struct student sd = {18, "james", 1.87 };
2. Define and initialize (one-time initialization)
Struct student sd2;
Sd2 = (struct student) {18, "james", 1.98 };
3. Define and initialize (initialize one by one)
Struct student sd3;
Sd3.name = "james ";
Sd3.age = 18;
Sd3.height = 1.87;
4. assign a value to the specified attribute
Struct student sd4 = {. height = 1.76,. age = 19,. name = "james "};

4. struct Array
Like struct variables, struct Arrays can also be defined in three ways:
Method 1:
Struct Student {
Char * name;
Int age;
};
Struct Student stu [5]; // defines 1

Method 2:
Struct Student {
Char * name;
Int age;
} Stu [5]; // defines 2

Method 3:
Struct {
Char * name;
Int age;
} Stu [5]; // defines 3

Struct array initialization:
Truct {
Char * name;
Int age;
} Stu [2] ={{ "Jmes", 21 },{ "JJ", 30 }};

5. struct Functions
When a struct variable is passed as a function parameter, the values of all Members are actually passed, that is, the values of the members in the real parameter are assigned to the corresponding parameter members one by one. Therefore, changes in the form parameters do not affect the real parameters. It is equivalent to copying.

// Define a struct
Struct Student {
Int age;
};
Void test (struct Student stu ){
Printf ("parameter before modification: % d", stu. age );
// Modify the age in the real Parameter
Stu. age = 10;
Printf ("modified form parameter: % d", stu. age );
}
Int main (int argc, const char * argv []) {
Struct Student stu = {30 };
Printf ("real parameter before modification: % d", stu. age );
// Call the test function
Test (stu );
Printf ("modified real parameters: % d", stu. age );
Return 0;
}

6. pointer to struct
Struct pointer variable definition form:Struct namePointer variable name *
With pointers to struct, there are three access methods for struct members.
Struct variable name. member name
(* Pointer variable name). Member name
Pointer variable name-> member name <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Keys + keys/w/sus8nUscP7PGJyIC8 + keys/w/spLrPJ1LHD + keys/keys = "Write picture description here" src = "http://www.bkjia.com/uploads/allimg/150805/0423391928-1.png" title = "\"/>

7. How to Create a bucket for a struct
It seems that the structured object is allocated to the bucket by adding the sum of the buckets occupied by all attributes and then allocating them.
Note:
In fact, in essence, the bucket allocated by the struct does not add the total storage space occupied by all attributes and then reallocate them.
Instead, it obtains the size of the attribute with the largest memory usage in the struct type, and then takes a multiple of the size.
Special case:
If the remaining storage space is "not enough" to store the data to be stored, the 8-byte storage space will be re-opened and the data to be stored will be put into the new storage space.
If the remaining storage space is enough to store the data to be stored, it will not be opened up.

8. Enumeration
Enumeration is specifically used to represent several fixed types of values. The essence of enumeration is the basic data type, that is, the integer type. It is very common in iOS development.
Format of Enumeration type definition
Enum Enumeration type name {
Type 1,
Type 2,
};
Note:Because the enumeration type is essentially an integer type, in addition to receiving fixed enumerated values, the enumeration type can also receive values of other integer types. By default, the enumerated values increase sequentially from 0.
General rules for defining enumeration:
* (1) values of the enumeration type generally start with k and follow the name of the enumeration type to keep up with the meaning of the current value
(2) Like a struct, the first letter of the name of the enumeration type is capitalized *
Enum Season
{
KSeasonSpring,
KSeasonSummer,
KSeasonAutumn,
KSeasonWinter
};

 

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.