C language Structure (STRUCT) detailed and sample code _c language

Source: Internet
Author: User

In the previous tutorial we explained the array, which is a set of data with the same type. But in the actual programming process, we also often need a group of different types of data, such as the Student Information registration form, the name is a string, the number is an integer, The age is an integer, the study group is a character, the score is decimal, because the data type is different, obviously can not use an array to store.

In C, a structure body (Struct) can be used to hold a set of different types of data. The structure body is defined in the form of:

struct structure Body name {
The variables or arrays contained in the structure body
};

A struct is a set that contains multiple variables or arrays, and their types can be the same or different, and each such variable or array is called a member of the struct body. Take a look at one of the following examples:

struct stu{
  char *name;//name
  int num;//student number
  int age;//ages
  char Group;//The Study Group
  float score;//Results
} ;

Stu is the name of the structure, which contains 5 members, name, NUM, age, group, score, respectively. Struct members are defined in the same way as variables and arrays, but cannot be initialized.

Note the semicolon following the curly brace; This is a complete statement.

A struct is also a data type that is defined by the programmer and can contain multiple other types of data.

such as int, float, char, etc. are the data types provided by the C language itself, which can no longer be split, which we call the basic data type, and the struct body may contain several basic types of data or other structures, which we call complex data types or constructed data types.

Structural Body variables

Since a struct is a data type, it can be used to define variables. For example:

struct Stu stu1, STU2;

Defines two variables stu1 and STU2, all of which are Stu types and consist of 5 members. Note that keyword struct can not be less.

Stu is like a "template", the defined variables all have the same properties. The structure can also be compared to the "drawing", the structural variables to "parts", according to the same drawings produced parts of the characteristics are the same.

You can also define struct variables while defining the structure:

Place the variable at the end of the definition of the struct body.

struct stu{
  char *name;//name
  int num;//student number
  int age;//ages
  char Group;//The Study Group
  float score;//Results
} STU1, STU2;

If you need only STU1, stu2 two variables, and you do not need to use the struct body name to define the other variables, you can also not give the struct name when you define it, as follows:

struct{//No write Stu
  char *name;//name
  int num;//student number
  int age;//ages
  char Group;//The Study Group
  float score; /Results
} stu1, STU2;

This is simple to write, but because there is no structural body name, there is no way to define new variables with that structure.

In theory, each member of the structure is stored continuously in memory, and the array is very similar, for example, the above structure variable STU1, STU2 memory distribution as shown in the following figure, the total occupy 4+4+4+1+4 = 17 bytes.

However, in the implementation of the compiler, there may be gaps between members, and for STU1, STU2, there are 3 bytes of blank padding between the member variable group and score (see figure below). In this way, STU1, STU2 actually occupy 17 + 3 = 20 bytes.

The reason for the "crack" between member variables is explained in detail in the section "C Language memory alignment, improving addressing efficiency" in the "C Language and Memory" topic.

Get and assign a member

Structural bodies and arrays are similar, but also a set of data, the overall use does not have much significance. arrays use subscript [] to get a single element, and the structure uses the dot number. Gets a single member. The general format for getting members of the struct body is:

Structural body variable name. member name;

In this way, you can get the values of members or assign values to members:

#include <stdio.h>
int main () {
  struct{
    char *name;//name
    int num;//School number
    int age;
    Group The group
    float score//Achievement
  } STU1;
  Assigning values to members of a struct
  stu1.name = "Tom";
  Stu1.num =;
  Stu1.age =;
  Stu1.group = ' A ';
  Stu1.score = 136.5;
  Read the value of the struct member
  printf ("%s's number is%d, age is%d, in%c group, this year's score is%.1f! \ n ", Stu1.name, Stu1.num, Stu1.age, Stu1.group, stu1.score);
  return 0;
}

Run Result:

Tom's School number is 12, the age is 18, in Group A, this year's score is 136.5!

In addition to being able to assign values to members, you can assign values as a whole when you define them, for example:

struct{
  char *name;//name
  int num;//School number
  int age;//ages
  char Group;//group
  float score;//Performance
} STU1, STU2 = {"Tom", A, ' A ', 136.5};

However, the overall assignment is limited to the definition of structural variables, in the use of the member can only assign values, and this array and the assignment is very similar.

It should be noted that the structure is a custom data type, is the template to create variables, do not occupy the memory space, the structure variables only contain real data, need memory space to store.

The above is the C language structure of the collation of data, follow-up continue to supplement the relevant information, thank you for your support for this site!

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.