"C-language summary" struct, union, enumeration

Source: Internet
Author: User

Structure: A collection with a value that cannot be typed
Union: Similar to structure, but sharing a storage space (only one member can be stored at a time)
Enumeration: is a type of shaping

Declaration of struct structure body

Structure Tag Declaration

struct part {    int number;    char name[256];};struct part part1;//不能去掉struct//也可以像下面的方式声明变量struct part {    int number;    char name[256];}part1, part2;

Using the definition of a struct type

//声明part类型的结构体typedefstruct {    int number;    char name[256];}part;part part1;//声明变量
struct as a parameter of a function
//打印结构体void printf_part(struct part p) {    printf("number is %d", p.number);    printf("name is %s", p.name);}
Declaration of an array of structures
struct part parts[100];//声明能装100个part类型变量的数组
Joint

Union and struct-like use, except that each union variable has a common storage space, changing the value of one variable will variable the value of other variables.
Just change the struct to union.

Enumeration

Statement:

enumflag {    false0,    true};
enum {    false,//枚举内的值会默认从0开始    true}flag;

"C-language summary" struct, union, enumeration

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.