C-language Classic---structural body

Source: Internet
Author: User

Structs also belong to constructed types. Structs are more flexible than arrays and can hold different types of variables


A struct is made up of groups of members that can be either basic data types or constructed types, such as arrays


struct belongs to custom data type


1. Structure declaration :

struct Structure Body name

{

type specifier member name;

...;

type specifier member name;

}; Note that this number cannot be reduced.

A struct is a custom type, and a struct variable is a variable that is decorated by a struct type.


Defining struct-Body variables

struct struct body name Variable name = {initial value};

struct member access

Representation of struct members

struct variable, member

typedef creates a new name, or type alias, for an existing type

typedef primitive Type New type

typedef int INTEGER

Such

int a = 10;

Can be written as:

Integer a = 10;

struct variable can be directly assigned value

Attention

Although arrays and structs are constructed types, struct variables can be assigned directly, and arrays cannot be directly assigned values.

Tips:

You can implement the direct assignment of an array by placing the array in the structure body

Structure Body Storage :

The space occupied by the structure can simply be considered as the sum of the space of the memory occupied by each member.

is actually the smallest integer multiple of the space occupied by the largest member variable

Arrays are split aligned when the number of bytes in this constructed type of array is greater than the bytes of the largest data type in the struct member variable

The essence of the memory alignment principle in a computer is for the CPU to be able to effectively access the relevant memory through the address.

Structure nesting

A member of a struct can still be a struct body

struct array

Multiple struct variables can be placed into an array, constituting an array of structs.

Example: struct Stu students[10]

Each struct variable can be accessed by subscript


C-language Classic---structural body

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.