[C language structure notes]

We all know the type of C language variable determines the variable storage space occupied. It can be declared as a float when we want to use a variable to hold the age can be declared as int type, when we want to use a variable to hold a particular subject test scores.

So, when we do a student information management system, the need to preserve the student's name, student number, age and other information, how to do it?

Such as when you want to save three student information,

Method One:

char *name1,*name2,*name3;  // 三个学生的姓名
int num1,num2,num3;         // 三个学生的学号
int age1,age2,age3;         // 三个学生的年龄

Second method:

// 创建结构体模板
struct student
{
  char *name;  // 学生名字
  int num;     // 学生学号
  int age;     // 学生年龄
};

// 使用该结构体模板创建三个变量stu1, stu2, stu3
struct student stu1, stu2, stu3;

Obviously, with the second method is more clear, because it put name, num, age are integrated in a template, use the time to apply as long as you can create a template. This is the structure.

What is the structure
structure (struct) is a collection of a series of the same type or different types of data consisting of data items, the data items referred to as members of the structure. As above student structure comprises three members, namely, name, num, age.

Type structure is a structure in C language. Data type of C language as shown below:

 

The method defined variable structure

method one:

// 创建结构体模板struct student
struct student{
  char *name;  // 学生名字
  int num;     // 学生学号
  int age;     // 学生年龄
};

// 使用该结构体模板创建三个变量stu1, stu2, stu3
struct student stu1, stu2, stu3;

Here, student is a structure name, the name is defined by arbitrary, but try to play a meaningful name. Which is equivalent to a template, you can use this template to define variables stu1, stu2, stu3. Do not forget the time defined struct.

Method Two:

// 定义三个结构体变量stu1, stu2, stu3
struct{
  char *name;  // 学生名字
  int num;     // 学生学号
  int age;     // 学生年龄
}stu1, stu2, stu3;

With respect to a method, the name of the structure is omitted here. Although more concise, but because there is no name, behind the structure can not be used to define a new variable.

Method three:

// 给结构体模板struct student重新命名为student
typedef struct student{
    char *name;  // 学生名字
    int num;     // 学生学号
    int age;     // 学生年龄
}student;

// 使用student创建三个结构体变量stu1, stu2, stu3
student stu1, stu2, stu3;

Use typedef struct student is defined here as a template structure an alias student, introduction to typedef the venue to the difference between #define and typedef? To view. Use typedef to create an alias structure, which in the actual programming is widely used, such as the STM32 microcontroller firmware library, a lot, as shown below:

Initialization structure

Initialize variables and arrays as follows:

int count = 0;
int arr[7] = {0,1,2,3,4,5,6};

Whether this structure variables can also be initialized? Yes, sure. And initialize the array syntax similar to:

struct student stu1 = {
  "ZhengNianJun", // 名字:正念君
  520,            // 学号:520
  23              // 年龄:23
};

In short, we use enclosed in curly braces in the initialization list to initialize, initialize each item separated by commas.

In order to allow the members of the association initialization and configuration items more obvious, let us initialize a separate line item for each member. We do this just to improve the readability of code to the compiler, just use a comma-separated keys to initialize each member.

C99 and C11 provide the specified initializer (designated initializer) is a structure. Initialized using dot operator and member name. For example, only members of the student name initialization structure, you can do this:
 

struct student stu1 ={
   .name = "ZhengNianJun"
};

Initializer can also be used to specify an arbitrary order:

struct student stu1 ={
   .age = 24,
   .num = 520,
   .name = "ZhengNianJun"
};

In addition, the first assignment of the final value of a particular member of it is actually obtained. For example, consider the following code:

struct student stu1 ={
  .num = 520,
  .name = "ZhengNianJun",
   1314
};

In this case, the value should be assigned to num 1314, immediately after the name because it is a member in the structure declaration. 1314 520 the new value replaces the previous.

Specified in the actual initialization it is also used to programming. As linux2.6.32.2 usb drive source (path: linux2.6.32.2 \ drivers \ usb \ storage \ usb.c), has the following code:

Access a structure member

Access member of the structure required by the structure member operator - dot (.). Such as:

student stu1; // 定义一个结构体变量stu1
stu1.name = "ZhengNianJun"; // 给stu1的成员name赋值
stu1.num = 520;             // 给stu1的成员num赋值
stu1.age = 23;              // 给stu1的成员age赋值

Examples

#include <stdio.h>

typedef struct student
{
 char *name;  // 学生名字
 int num;     // 学生学号
 int age;     // 学生年龄
}student;

int main(void)
{
 student stu1;  // 定义一个结构体变量stu1
 
 /* 给结构体变量stu1的成员进行赋值 */
 stu1.name = "ZhengNianJun";
 stu1.num = 520;
 stu1.age = 23;
 
 printf("\n============================================\n");
 printf("My name is %s\n", stu1.name);
 printf("My num is %d\n", stu1.num);
 printf("My age is %d\n", stu1.age);
 printf("欢迎关注正念君编程学习笔记!\n", stu1.age);
 printf("============================================\n");

 return 0;
}

 

As a result of the program:

 

A structure is a very important element, used a lot. Memory on the structure alignment can view the notes to the period: [notes] C language structure memory alignment problem

These are some notes about the structure, if wrong welcome that. Thank you!

Welcome to micro-channel or two-dimensional code scanning left side zhengnian-2018 concerned about my micro-channel public number to see more programming study notes in the micro-letter search!



Original link: https: //blog.csdn.net/zhengnianli/article/details/87568902

Published 245 original articles · won praise 95 · views 120 000 +

Guess you like

Origin blog.csdn.net/ywl470812087/article/details/103440273