C language structures must be known

Source: Internet
Author: User

Structural body

A struct is a special type that can be packaged with other types as a composite type. In the object-oriented concept, it is a special class.

Use several forms of structure:

First Kind

Define the struct body first, and then define the struct variable.

Define the structure body:

struct point1{    int x;    int y;};

Defining struct-Body variables

struct point1 point;
The second Kind

Define an anonymous struct, and then define struct-body variables

struct{    int x;    int y;}point2;
Third Kind

Define struct-body variables while defining structs

struct point3{    int x;    int y;}point;
Fourth type

Defining structs with typedef

typedefstruct point4{    int x;    int y;}t_point;

Then define the structure variables with T_point

point;
struct array
    struct student{        int age;        char *name;    };    struct student ss[10];
struct-Body pointer
    struct student *pst;    pst = &foo;
struct initialization

There are structural definitions

    struct student{        int age;        char *name;    };
First Kind
    struct student foo1 = {11"xiaoming"};    struct student foo2 = {11}
The second Kind
    struct student foo3 = {.age = 11};
Third Kind
    struct student foo4 = (struct student){11"xiaoming"};    struct student foo5 = (struct11};
struct array Initialization
    struct student ss1[10{0};    struct student ss2[10{{}{}{}};    struct student ss3[10{[2] = {}, [3{}};    struct student ss4[10{[2].age = 10, [3].name = "xiaoming"};
accessing struct members

Use the "." return struct member

    struct student foo = {11"xiaoming"};    int age = foo.age;    *name = foo.name;    printf("age is %d, name is %s\n", age, name);    20;    "liyong";    printf("age is %d, name is %s\n", foo.age, foo.name);

When using the struct pointer, you can use the arrow operator

    *pst;    pst = &foo;    printf("pst age is: %d and name is %s\n", (*pst).age, (*pst).name);    printf("pst age is: %d and name is %s\n", pst->age, pst->name);

C language structures must be known

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.