Structure in C language

Source: Internet
Author: User

    • Definition of struct type:
1 struct Student 2 {3    int  4    char name[]; 5 6 };

Note: Student is a custom data type, not a variable like Int,char is the basic data type,

    • Definitions and references for struct variables:
1 struct  2{3     int  ID; 4     Char name[]; 5 //Direct definition of two structural body variables

1Student Mike ={123,{'m','I','k','e'}};//defining variables and initializing them2Mike.id =20130000+ mike.id//Use3  for(inti =0; Mike.name[i]! =' /'; i++)4mike.name[i]=ToUpper (Mike.name[i]);5cout<<"ID:"<<mike.id<<"Name:"<<mike.name<<endl;//
    • Storage of struct variables:

A structure variable occupies a contiguous memory space.

    • Assignment of struct variables:
1 student Mike ={123, {'m','i',' k ','e'}; 2 student Lily; 3 lily = Mike; //Can be directly assigned, the corresponding variable is also assigned a value
    • Structure variables as parameters of functions

The same as the variables do the function parameters, the parameters and arguments directly to the value of the transfer, copy only one copy, and the array is different from the parameter

    • struct variable as return value of function

is also a copy of copy to assign value

    • Pointer to struct body:
Student mike={123,{'m','I','k','e'}};student*ps = &Mike; cout<<"ID:"<< (*PS) .id<<"Name:"<< (*PS) .name<<Endl;cout<<"ID:"<<ps->id<<"Name:"<<ps->name<<endl;
    • struct array:
1 struct  2{3     int  ID; 4     Char name[]; 5 } stu[];

    • Summary: The properties of the struct data type are consistent with the characteristics of the normal data type

Structure in C language

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.