Struct, C structure

Source: Internet
Author: User

Struct, C structure

Link: http://www.orlion.ga/758/

struct point {    double x, y;};

In this way, the identifier point is defined. Since it is an identifier, its naming rules are the same as variables, but not a variable but a type. This identifier is a Tag in the C language, the entire struct point {double x, y;} can be considered as a type name, just like int or double, but this is a composite type. If you use this type name to define variables, you can write:

struct point {    double x , y;} p1 , p2;

In this way, p1 and p2 are the variable names. ";" must be added after the variable definition, and ";" cannot be omitted after the struct definition (such as the first code.

Regardless of the method used to define the point Tag, you can directly use struct point to replace the type name. For example, you can re-define two variables as follows:

struct point p3 , p4;

If a variable is defined when the struct type is defined, you do not need to write a Tag. For example:

struct {    double x , y;} p1 , p2;

Struct variables can be accessed using the "." OPERATOR:

# Include <stdio. h> int main (void) {struct point {double x, y;} p; double x = 1.0; p. x = x; z. y = 2.0; struct point p2; printf ("z x: % f, y: % f \ n", z. x, z. y); return 0 ;}

Struct can also be initialized during definition, for example:

struct point p = {1.0 , 2.0};

If the data in the brackets is less than that in the struct, the unspecified members are initialized with 0, just like the uninitialized global variables. Struct value assignment is somewhat different from the basic data type.ErrorOf:

struct point p;p = {1.0 , 2.0};

However, struct point p1 = {1.0, 2.0}; struct point p2 = p1; p1 = p2; this way.

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.