C + + structural Body (iii)

Source: Internet
Author: User

Three ways to define a struct variable

First, define the struct type, and then define the struct-body variable

General form:

struct STU_INFO
{
Char name[12];
int num;
Char id_card[19];
int G_score;
int X_score;
int S_score;
};

Defining struct-Body variables stu1,stu2

struct Stu_info stu1, stu2;


The second method, defining struct-body types while defining struct-body variables

General form:

struct STU_INFO
{
Char name[12]; /* Student Name */
int num; /* Student Number */
Char id_card[19]; /* ID Number */
int M_score; /* Math Results */
int C_score; /* Language score */
int H_score; /* Historical Results */
}STU1,STU2;


The third method, directly defines the structure of the variable

struct
{
Char name[12]; /* Student Name */
int num; /* Student Number */
Char id_card[19]; /* ID Number */
int M_score; /* Math Results */
int C_score; /* Language score */
int H_score; /* Historical Results */
}STU1,STU2;

When you define a variable directly with a nameless struct, you can only define it once, and this method is not recommended.


Storage structure of struct variables:

1. Structure variable storage structure: In general, each member is stored continuously, occupying space for the sum of the space occupied by each member.

2. It is common to use sizeof to calculate the size of a struct or struct variable that requires space to be allocated, regardless of specific details.

C + + struct Body (iii)

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.