结构体struct的定义和使用

前言

本文详细介绍了关于struct的多种定义方式,以及不同定义方式在C和C++中的使用方法。

本文涉及到的代码示例可在我的github获取
地址:struct的使用

一、结构体的定义方式

1、普通形式,只定义结构体

struct type_one_s{
    
    
    int a;
    int b;
};

这种形式只定义了一个结构体type_one_s,此时的type_one_s相当于一个类型名。
C和C++中使用区别:

  • 在C中使用type_one_s定义结构体变量时,需要在前边加上struct,即:
struct type_one_s type_one1;
  • 在C++中使用type_one_s定义结构体变量时,不需要在前边加struct,即:
type_one_s type_one1;

2、定义结构体的同时也定义结构体变量

struct type_two_s
{
    
    
    int a;
    int b;
}type_two1;

这种形式的使用与第一种形式相同,即定义了一个结构体type_two_s,只是在定义结构体的同时也定义了一个结构体变量type_two1,后续可以直接使用结构体变量type_two1;

3、定义结构体的时候缺失结构体名,同时定义结构体变量

struct {
    
    
    int a;
    int b;
}type_three1,type_three2;

这种形式只能使用在定义结构体的同时定义的结构体变量type_three1type_three2,由于没有结构体名,因此后续不可以再定义新的结构体变量。

4、使用typedef, 定义结构体的同时为结构体取别名

typedef struct type_four_s {
    int a;
    int b;
}type_four_alias_s;

使用这种形式,定义了结构体type_four_s,并为结构体设置别名为type_four_alias_s,使用结构体名定义结构体变量时,C需要加struct,C++不需要;使用结构体别名定义结构体变量时,C和C++都不需要加struct

C中:

//使用结构体名定义结构体变量,需要加struct
struct type_four_s type_four1 = {
    
    13,14};   
//使用结构体别名定义结构体变量,不需要加struct   
type_four_alias_s type_four2 = {
    
    15,16};      

C++中:

//使用结构体名定义结构体变量,不需要加struct
type_four_s type_four1 = {
    
    13,14};     
//使用结构体别名定义结构体变量,不需要加struct       
type_four_alias_s type_four2 = {
    
    15,16};      

5、使用typedef,定义结构体缺失结构体名,只取别名

typedef struct{
    
    
    int a;
    int b;
}type_five_alias_s;

这种形式定义了一个缺失结构体名的结构体,但使用typedef为结构体设置了别名,在C和C++中,使用别名创建结构体变量,均不需要加struct

C中:

// 赋值方式一
type_five_alias_s type_five1 = {
    
    17,18};
// 赋值方式二
type_five_alias_s type_five2;
type_five2.a = 19;
type_five2.b = 20; 

C++中:

// 赋值方式一
type_five_alias_s type_five1 = {
    
    17,18};
// 赋值方式二
type_five_alias_s type_five2;
type_five2.a = 19;
type_five2.b = 20; 

二、结构体的使用

1、C和C++中定义结构体5种形式的头文件usestruct.h:

#ifndef USESTRUCT_H
#define USESTRUCT_H

// ################ type one  ###############
// 定义结构体type_one_s,此时结构体相当于一个类型
// 使用形式: type_one_s type_one1;
// C和C++中均可使用,在C中使用定义时,必须加struct,C++中不需要
struct type_one_s{
    
    
    int a;
    int b;
};
// ################ type one end  ############

// ################ type two  ################
// 定义结构体type_two_s,同时定义需要使用的变量type_two1;
// 如果之后还需要定义其他变量,定义形式:type_two_s type_two2; 
// 其实和上边的type one是一样的,不同的只是这种形式在定义结构体的同时定义了结构体变量
// C和C++中均可使用,使用方式同type one
struct type_two_s
{
    
    
    int a;
    int b;
}type_two1;
// ################ type two end  ############


// ################ type  three   ############
// 定义结构体时,没有结构体名称,同时定义结构体变量type_three1,type_three2。
// 只能使用在定义结构体的同时定义的结构体变量,之后不能再定义别的结构体变量。
// C和C++中均可使用
struct {
    
    
    int a;
    int b;
}type_three1,type_three2;
// ################ type  three  end  #########


// ################ type  four   #############
// 定义结构体时,同时为结构体设置别名
// 可以使用结构体名和结构体别名定义结构体变量
// 使用结构体名定义变量时,C中需要加struct,C++不需要
// 使用结构体别名定义变量时,C和C++都不需要加struct
typedef struct type_four_s {
    
    
    int a;
    int b;
}type_four_alias_s;
// ################ type  four  end  #########

// ################ type  five type  #########
// 定义结构体时,缺失结构体名,只为结构体设置别名
// 可以使用结构体别名定义结构体变量
// 使用结构体别名定义变量,C和C++都不需要加struct
typedef struct{
    
    
    int a;
    int b;
}type_five_alias_s;
// ############### type  five  end  ##########

#endif

2、C中使用结构体的源文件main.c:

#include "stdio.h"
#include "usestruct.h"

int main(){
    
    
    printf("Hello struct of C !\n");

    // type 1,在C中定义必须加struct
    struct type_one_s type_one1 = {
    
    1,2};

    struct type_one_s type_one2;
    type_one2.a = 3;
    type_one2.b = 4;

    printf("type 1 : type_one1.a = %d ,type_one1.b = %d\n",type_one1.a,type_one1.b);
    printf("type 1 : type_one2.a = %d ,type_one2.b = %d\n",type_one2.a,type_one2.b);
    printf("=======================================================\n");
    // type 2,与type 1 用法一致,但可直接使用在定义结构体时定义的结构体变量

    type_two1.a = 5;
    type_two1.b = 6;

    struct type_two_s type_two2 = {
    
    7,8};
    printf("type 2 : type_two1.a = %d ,type_two1.b = %d\n",type_two1.a,type_two1.b);
    printf("type 2 : type_two2.a = %d ,type_two2.b = %d\n",type_two2.a,type_two2.b);
    printf("=======================================================\n");
    // type3, 在定义结构体时无结构体名称,只能使用已定义的结构体变量
    type_three1.a = 9;
    type_three1.b = 10;
    type_three2.a = 11;
    type_three2.b = 12;
    printf("type 3 : type_three1.a = %d ,type_three1.b = %d\n",type_three1.a,type_three1.b);
    printf("type 3 : type_three2.a = %d ,type_three2.b = %d\n",type_three2.a,type_three2.b);
    printf("=======================================================\n");

    //type4, 在定义结构体时添加typedef修饰为结构体取别名
    struct type_four_s type_four1 = {
    
    13,14};      //使用结构体名定义结构体变量,需要加struct
    type_four_alias_s type_four2 = {
    
    15,16};      //使用结构体别名定义结构体变量,不需要加struct
    printf("type 4 : type_four1.a = %d ,type_four1.b = %d\n",type_four1.a,type_four1.b);
    printf("type 4 : type_four2.a = %d ,type_four2.b = %d\n",type_four2.a,type_four2.b);
    printf("=======================================================\n");

    // type5, 在定义结构体时省略结构体名,只取别名
    type_five_alias_s type_five1 = {
    
    17,18};
    type_five_alias_s type_five2;
    type_five2.a = 19;
    type_five2.b = 20; 
    printf("type 5 : type_five1.a = %d ,type_five1.b = %d\n",type_five1.a,type_five1.b);
    printf("type 5 : type_five2.a = %d ,type_five2.b = %d\n",type_five2.a,type_five2.b);
    printf("=======================================================\n");
}

3、C++中使用结构体的源文件main.cpp:

#include<iostream>
#include "usestruct.h"

using std::cout;
using std::endl;

int main(){
    
    
    cout<<"hello struct of C++ !"<<endl;

    // type one,在C++ 中定义不需加struct
    // 结构体赋值方法一,使用花括号
    type_one_s type_one1 = {
    
    1,2};  

    // 结构体赋值方法二,直接赋值
    type_one_s type_one2;
    type_one2.a = 3;
    type_one2.b = 4;

    cout<<"type one : type_one1.a ="<<type_one1.a<<"  type_one1.b ="<<type_one1.b<<endl;
    cout<<"type one : type_one2.a ="<<type_one2.a<<"  type_one2.b ="<<type_one2.b<<endl;
    cout<<"======================================================="<<endl;

    // type two,与type one 用法一致,但可直接使用在定义结构体时定义的结构体变量
    type_two1.a = 5;
    type_two1.b = 6;

    type_two_s type_two2 = {
    
    7,8};
    cout<<"type two : type_two1.a ="<<type_two1.a<<"  type_two1.b ="<<type_two1.b<<endl;
    cout<<"type two : type_two2.a ="<<type_two2.a<<"  type_two2.b ="<<type_two2.b<<endl;
    cout<<"======================================================="<<endl;

    // type3, 在定义结构体时无结构体名称,只能使用已定义的结构体变量
    type_three1.a = 9;
    type_three1.b = 10;
    type_three2.a = 11;
    type_three2.b = 12;
    cout<<"type three : type_three1.a ="<<type_three1.a<<"  type_three1.b ="<<type_three1.b<<endl;
    cout<<"type three : type_three2.a ="<<type_three2.a<<"  type_three2.b ="<<type_three2.b<<endl;
    cout<<"======================================================="<<endl;

    //type4, 在定义结构体时添加typedef修饰,为结构体取别名
    type_four_s type_four1 = {
    
    13,14};            //使用结构体名定义结构体变量,不需要加struct
    type_four_alias_s type_four2 = {
    
    15,16};      //使用结构体别名定义结构体变量,不需要加struct
    cout<<"type four : type_four1.a ="<<type_four1.a<<"  type_four1.b ="<<type_four1.b<<endl;
    cout<<"type four : type_four2.a ="<<type_four2.a<<"  type_four2.b ="<<type_four2.b<<endl;
    cout<<"======================================================="<<endl;

    // type5, 在定义结构体时省略结构体名,只取别名
    type_five_alias_s type_five1 = {
    
    17,18};
    type_five_alias_s type_five2;
    type_five2.a = 19;
    type_five2.b = 20; 
    cout<<"type five : type_five1.a ="<<type_five1.a<<"  type_five1.b ="<<type_five1.b<<endl;
    cout<<"type five : type_five2.a ="<<type_five2.a<<"  type_five2.b ="<<type_five2.b<<endl;
    cout<<"======================================================="<<endl;
}

猜你喜欢

转载自blog.csdn.net/mataojie/article/details/121671343