The use of structural body in ios-object-c learning

Source: Internet
Author: User

Preface: defining A struct does not define a variable, but rather defines the type of data.

Structure function:

Structs are the same as other types of underlying data types, such as int types, and the char type is just a struct that can be made into the type of data you want.  To facilitate future use. In the actual project, there is a large number of structures. Developers often use structs to encapsulate properties to form new types.  Because the C language internal program is relatively simple, the developers often use the structure to create a new "attribute", the purpose is to simplify the operation. The function of the structure is not simple, its main function is encapsulation. The advantage of encapsulation is that it can be reused. Let the user not care about what this is, as long as it is used according to the definition.

Excerpt from: Baidu Encyclopedia

I. Define the structure of the body

Basic syntax:

struct struct-Body name {    type name;    type name;    type name;    ... };

There are three ways to define the structure, please continue to look down.

Define method One:

struct Programmer {    char *name;     int Age ;     int phone;};

Initialization

struct Programmer protest = {"Garvey"13823992034 "};

Define method Two:

struct {    int age ;     int tag;} str1;

Initialize, this can only use point syntax, please look down.

Define method Three:

struct {    char *name;     int tag;} Structext;

Using typedef to create a new type, you can now define a new struct variable using Structext.

We can see the difference between method one and method two. Method one declares the struct name, but does not declare the struct-body variable. Method two declares the struct variable, but does not declare the struct body name. So we can know: " struct names and struct variables must either be one or both ."

struct names and struct variables are present, for example:

struct Programmer {    char *name;     int Age ;     Char *phone;} prosuccess, Profail;

Second, the structure member accesses and assigns the value

Access and assignment are all using point syntax

Access:

Prosuccess.name;prosuccess.age;prosuccess.phone;

Assignment/modification:

" Calvin "  "13138940202";

Third, the attention point

Error initialization, this sentence will be at the time of pre-compilation error:

prosuccess = {"name"ten"13732423452"};

Write so much, have not output any content, here to say is. Use the variable defined by char to use%s on output. Cases:

NSLog (@ "%s-%d-%s", Protest.name, Protest.age, Protest.phone);

PS: Variables defined using char, cannot store Chinese (because I do not know how to output, the output will become a code, you can not understand).

Well, write so much, I want to rest, the eyes are very tired ...

The use of structural body in ios-object-c learning

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.