C language (vi) structural body

Source: Internet
Author: User

I. Overview of structural bodies

1. Introduction

A struct is a data type that stores elements that can make different data types.

The constituent elements of a struct, commonly called struct members.

2. Definition

struct struct body name {        type name 1 member name 1;        Type Name 2 member name 2;        ...   Type name n member name N;    };
3. Defining the structure type

struct Student {    char *name;//name    int age;//ages    float height;//height};
4. Defining struct variables

1) Define the struct type first, then define the variable

2) define a variable while defining the struct type

3) directly define struct type variable, omit struct name

struct Student {    char *name;    int age;}; struct Student stu; or struct Student {    char *name;    int age;} Stu, or struct {    char *name;    int age;} Stu

5. Attention Points

1) recursive definition of struct itself is not allowed

The following wording is wrong.

struct Student {     int age;     struct Student Stu;};

2) The structure body can contain other structures

struct Date {    int year;    int month;    int day;}; struct Student {    char *name;    struct Date birthday;};

3) defines the struct type, only illustrates the composition of the struct type, and does not allocate storage space for it, as if the system does not allocate storage space for the int itself. Storage space is allocated only if a variable of the struct type is defined.

struct Student {    char *name;    int age;}; struct Student stu;
The system will allocate storage for the variable only if it is executed to the row that defines the struct body variable.

4) The storage space occupied by the struct type variable is the sum of the memory of its members, and the individual members are sorted sequentially in memory in the order defined.

struct Student {   char *name;//name   int age;//ages   float height;//height};
in a 16-bit compiler environment, a student variable consumes memory: 2+2+4=8 bytes.

6. Initialization of the structure

The initial values of each member are placed sequentially in "{}", separated by commas, and assigned to each value.

struct Student {    char *name;    int age;}; struct Student stu = {"MJ", 27};<span style= "Font-family:consolas; Background-color:rgb (255, 255, 255); " ></span>

The initialization assignment and the definition of the variable cannot be separated only when the variable is defined.
struct Student stu;
stu={"MJ", 26}; it's wrong.

second, the use of structural bodies

1. Structural body

1) Generally, the operation of the structure variable is in the member unit, the general form of the reference is: struct variable name. Member Name

#include <stdio.h>struct Student{char *name;int age;}; int main () {struct Student stu={"Zhangsan", 29};struct Student stu2;stu2.name= "Lisi"; stu2.age=30;printf ("%s\n", Stu2.name);p rintf ("%d\n", stu2.age); return 0;}
2) If a member is also a struct variable, you can access the lower-level members with continuous and private member operators.
#include <stdio.h>struct Date {     int year;     int month;     int day;}; struct Student {    char *name;    struct Date birthday;}; int main () {struct Student stu;stu.birthday.year = 1986;stu.birthday.month = 9;stu.birthday.day = 10;return 0;}

3) Overall assignment between struct variables of the same typestruct student{...};
struct Student stu1 ={...};
struct Student stu2 = stu1;   
2. Structure array

1) There are three ways to define an array of structures

struct Student {    char *name;    int age;}; struct Student stu[5]; Define 1struct Student {    char *name;    int age;} STU[5]; Define 2struct {    char *name;    int age;} STU[5]; Definition 3
2) Initialization of the structure array

struct {    char *name;    int age;} Stu[2] = {"MJ", +}, {"JJ", 30}};

3. struct as function parameter

When a struct variable is passed as a function parameter, the value of all the members is actually passed. That is, the value one by one of the member in the argument is assigned to the corresponding parameter member. Therefore, the change of the formal parameter does not affect the argument.

#include <stdio.h>struct Student{char *name;int age;}; void Test (struct Student stu) {printf ("%d\n", Stu.age);p rintf ("%s\n", stu.name); Stu.name= "Xiaosi";//modify attribute stu.age=30; printf ("%d\n", Stu.age);p rintf ("%s\n", Stu.name);} int main () {struct Student stu1[2]={{"Zhangsan", 20},{"Lisi", 25}};//use struct array test (stu1[0]);//The parameter passed is the first element of the struct array}

4. Pointers to struct bodies

1) Each struct variable has its own storage space and address, so pointers can also point to struct variables.2) Structure pointer variable definition form, struct struct name * pointer variable name3) with pointers to structs, there are three ways to access struct members struct variable name. Member name
{* pointer variable name}. Member name
pointer variable name, member name
#include <stdio.h>//defines a struct type int main () {struct Student{char *name;int age;};/ /define a struct variable struct Student stu1={"Zhangsan", 21};//define a pointer variable to a struct struct Student *p;//initialize pointer variable p=&stu1;printf to struct body ( "Name=%s,age=%d\n", (*p). Name, (*p). Age);p rintf ("name=%s,age=%d\n", P->name,p->age);p rintf ("name=%s,age=%d\ n ", stu1.name,stu1.age); return 0;}


































3. The same type of structure variables can be assigned to the overall valuestruct student{...};
struct Student stu1 ={...};
struct Student stu2 = stu1;  

C language (vi) structural body

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.