IOS-Object-C learning structure usage
Defining struct is not a variable, but a data type. Struct: struct is the same as other basic data types, such as int and char, but struct can be the data type you want. To facilitate future use. A large number of struct exist in actual projects. Developers often use struct to encapsulate some attributes to form a new type. Because the internal program of C language is relatively simple, developers usually use struct to create new "attributes", which aims to simplify operations. Struct does not play a simple role in functions. Its main role is encapsulation. The advantage of encapsulation is that it can be reused. Users do not have to worry about what this is, as long as they can be used according to definitions. From: Baidu Baike 1. Basic syntax for defining struct: struct name {type name;...}; three methods for defining struct: continue. Definition Method 1: struct programmer {char * name; int age; int phone ;}; initialization: struct programmer proTest = {"Garvey", 28, "13823992034 "}; method 2: struct {int age; int tag;} str1; Initialization. Only the dot syntax can be used. For details, refer. Method 3: typedef struct {char * name; int tag;} structExt; Use typedef to create a new type. Now you can use structExt to define a new struct variable. We can see the differences between method 1 and method 2. Method 1 declares the struct name, but does not declare the struct variable. Method 2 declares the struct variable, but does not declare the struct name. Therefore, we can know: "The struct name and struct variable must have one or both ". Struct names and struct variables exist, for example, struct programmer {char * name; int age; char * phone;} proSuccess, proFail; 2. Access by struct members, access by assignment, and access by assignment using point Syntax: proSuccess. name; proSuccess. age; proSuccess. phone; Value assignment/modification: proSuccess. name = "Calvin"; proSuccess. age = 27; proSuccess. phone = "13138940202"; 3. Pay attention to the incorrect initialization method. This statement will report an error during pre-Compilation: proSuccess = {"name", 10, "13732423452 "}; I haven't output any content after writing so much. What I want to talk about here is. Use the variable defined by char, and use % s for output. For example, NSLog (@ "% s-% d-% s", proTest. name, proTest. age, proTest. phone); PS: variables defined by char cannot be stored in Chinese (because I don't know how to output them, the output will become a code, and you don't understand it at all ).