C language learning in ios development-structure introduction and ios Language Learning

Source: Internet
Author: User

C language learning in ios development-structure introduction and ios Language Learning

In the development process, a group of different types of data is often needed, such as the student's personal information, which consists of the name, age, gender, and height, because the data is composed of different data types, it cannot be represented by arrays. For a group of data of different data types, struct can be used for storage. Of course, for object-oriented languages, it is best to use classes, but the C language is process-oriented. Therefore, the structure is used for representation.

1. struct Definition
Struct name {type name member name 1; type name member name 2;... type name member name n ;};

 

2. struct variable Declaration
1. First define the struct type, then define the variable

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** define the structure of student information */struct student {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height}; int main (int argc, const char * argv []) {// declare the structure variable struct student student1; struct student student2; return 0 ;}

 

III.Define both struct types and variables

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** defines the structure of student information and declares two student Structure Variables student1 and student12 */struct student {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height} student1, student2; int main (int argc, const char * argv []) {return 0 ;}

 

4.Directly define struct type variables, omitting the type name

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** directly declare two struct variables student1 and student2 */struct {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height} student1, student2; int main (int argc, const char * argv []) {return 0 ;}
 

 

5. Nesting of struct
1 Struct can contain but cannot be used recursively.

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** defines the date struct */struct date {unsigned int year; unsigned int month; unsigned int day ;}; /*** define the student struct */struct student {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height struct date birthday; // date Of Birth (date struct)}; int main (int argc, const char * argv []) {return 0;

 

6. struct Initialization

<1> struct variables can be initialized to multiple members at a time during declaration. However, it must be noted that the initialization sequence must be the same as the sequence in which struct members are defined, the number of initialized members can be less than the total number of members.

<2> after declaring a structure variable, you can use the structure variable name. member name to assign values or values to it.

<3> after declaring a structure variable, you can receive values of other structure variables of the same type.

Code

/// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** defines the date struct */struct date {unsigned int year; unsigned int month; unsigned int day ;}; /*** define the student struct */struct student {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height struct date birthday; // birthdate}; int main (int argc, const char * argv []) {// <1> assign struct date birth1 = {1992, 1, 1} to multiple members at a time; struct student student1 = {"jredu", 21, 'F ', 180.0, birth1}; // <2> assign student1.age = 20 to a single member; student1.height = 178.0; // <3> assign a value to all variables of the same type, struct student student2 = student1; return 0 ;}

 

7. Use of struct

Struct is a custom data type, but it is actually used the same as the basic data type provided to us by the system. Therefore, apart from using the structure as a variable, you can also use struct as arrays, pointers, and functions.

1 Structure Array

An array is used to store a group of struct-type variables, for example, a group of students.

When using the structure array, you can obtain the structure array in the same way as the structure variable mentioned above.

Code

/*** <1> first define the struct */struct student {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height}; int main (int argc, const char * argv []) {// declare the structure array struct student stus [10]; return 0 ;}

 

Code

/*** <2> define the struct and directly declare the structure array */struct student {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height} stus [10];

 

Code

/*** <3> directly declare the structure array */struct {char name [100]; // name unsigned int age; // age char sex; // gender double height; // height} stus [10];

 

2 Pointer to struct

To use pointers to indirectly change data, you must use pointers of the same type to point to objects. When using a struct type variable or array, you must use a struct type pointer.

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** define struct */struct student {char * name; // name unsigned int age; // age}; int main (int argc, const char * argv []) {// declare the structure variable struct student student1 = {"jredu", 21}; // define a structure pointer struct student * ptr = & student1; // access structure members, such as obtaining student information // Method 1: directly use the structure variable printf ("name = % s, age = % u \ n", student1.name, student1.age ); // Method 2: Get the structure variable printf ("name = % s, age = % u \ n", (* ptr) through the pointer ). name, (* ptr ). age); // method 3: directly use the pointer printf ("name = % s, age = % u \ n", ptr-> name, ptr-> age ); return 0 ;}

 

3 Struct function parameters

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** define struct */struct student {char * name; // name unsigned int age; // age}; void func1 (struct student tempStu ); void func2 (struct student * ptrStu); int main (int argc, const char * argv []) {// declare the structure variable struct student student1 = {"jredu", 21 }; struct student student2 = student1; // call the function func1 (student1) whose parameter is the structure variable; printf ("student1 name = % s \ n", student1.name ); // call the function func2 (& student2) whose parameter is the structure variable; printf ("student2 name = % s \ n", student2.name); return 0 ;} void func1 (struct student tempStu) {tempStu. name = "apple";} void func2 (struct student * ptrStu) {ptrStu-> name = "apple ";}

 

8. Simplified struct

Typedef can rename the data type, so you can use it to simplify the operation when defining the struct.

Code

//// Main. c // struct /// Created by jerei on 15-12-27. // Copyright (c) 2015 jerehedu. all rights reserved. // # include <stdio. h>/*** define struct */typedef struct {char * name; // name unsigned int age; // age} Student; int main (int argc, const char * argv []) {// declare the schema variable Student student1 = {"jredu", 21}; return 0 ;}

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.