Structure of C language

Source: Internet
Author: User

What is a structural body?

In simple terms, a struct is a structure that can contain different data types, it is a kind of data type that can be defined by itself, its characteristics and array are mainly two different, first, the structure can declare different data types in a structure, and the structure variables of the second same structure can be assigned to each other. and arrays can't be done, because an array is a data collection of a single data type, which itself is not a data type (while the struct is), the array name is a constant pointer, so it cannot be performed as a left value, so the arrays cannot be copied from one to the other, even if the data type and array size are identical.

The structure body is defined using the struct modifier, for example:

struct test
{
float a;
int b;
};

The above code defines a struct named test, its data type is test, it contains two members A and B, member A's data type is floating-point, member B's data type is integral type.

Because the structure itself is a custom data type, the method of defining the structural body variable is the same as the method of defining the ordinary variable.

Test PN1;

In this way, we define the structure variable PN1 of the structure body data type, and the access of the structure member through the point operator, pn1.a=10 the assignment operation to the member a of the pn1 of the structural body variable.

Note: The structure life itself does not occupy any memory space, and the computer allocates memory only when you define structural variables with the type of struct you define.

Structure, which can also define pointers, then the structure pointer is called a structure pointer.

The structure pointer accesses the member through the-> symbol, and below we look at a complete example:

#include <ioseam>
#include <sing>
using namespace s;

struct test//定义一个名为test的结构体
{
int a;//定义结构体成员a
int b;//定义结构体成员b
};

void main()
{
test pn1;//定义结构体变量pn1
test pn2;//定义结构体变量pn2

pn2.a=10;//通过成员操作符.给结构体变量pn2中的成员a赋值
pn2.b=3;//通过成员操作符.给结构体变量pn2中的成员b赋值

pn1=pn2;//把pn2中所有的成员值复制给具有相同结构的结构体变量pn1
cout<<pn1.a<<"|"<<pn1.b<<endl;
cout<<pn2.a<<"|"<<pn2.b<<endl;

test *point;//定义结构指针

point=&pn2;//指针指向结构体变量pn2的内存地址
cout<<pn2.a<<"|"<<pn2.b<<endl;
point->a=99;//通过结构指针修改结构体变量pn2成员a的值
cout<<pn2.a<<"|"<<pn2.b<<endl;
cout<<point->a<<"|"<<point->b<<endl;
cin.get();
}

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.