struct, Union, enum

Source: Internet
Author: User

struct

1, structure and array of differences: can be declared in the structure of the array; struct variables can be assigned to each other, and arrays are not.

2. The difference between struct and class: class's member access rights are private by default, while struct members are public.

3, the definition of structural body:

(1) can be recursive. Inside a struct, you can use pointers to point to yourself. For example, a linked list.

(2) can be nested. A struct can contain other structures within it.

4, the bit field in the structure.

When storing information, you do not need to occupy a full byte, but only a few or one bits.


Union

Structs and common bodies are composed of several different types of data type members, but at any one time, only one of the selected members is stored in the common body. and the structural body

in all the members are pure. For different member assignments of a common body, overrides are made to the other members, and the value of the original member does not exist.

(1) The structure occupies memory, which may exceed the amount of the memory of each member, while the shared body occupies the maximum memory for each member.

(2) The order in which union and struct is stored in memory is stored from the ground address.

big-endian storage: High bytes of data are stored in low addresses. Small-End Storage: Low bytes of data are stored in low addresses.


Enum

(1) Definition

Enum Enum type name
{
Enumerate tables
}


For example:

Enum COLORENUM1
{

Red,//Note that the system assigns a value of 0 to it by default

Blue,//The system is assigned a value of 1

Black,//system is assigned a value of 2

Pink//System is assigned a value of 3

}


Enum COLORENUM2
{

Red = 1,//The user assigns the value itself so the enumeration is a set of symbolic name/value pairs

Blue,//The system is assigned a value of 2

Black = 1,//user-assigned value 1

Pink//System is assigned a value of 2

}

As you can see, the system assigns a value to a constant that is not assigned, but the value is assigned in the same way as the previous value +1.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

struct, Union, enum

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.