struct结构体在c和c++中的差别

非常多次遇到这个struct的问题,今天在这里简单总结一下我的理解

一、struct在C 中的使用

1、单独使用struct定义结构体类型

struct Student {
   int id;
   int name;
}stu1;
struct Student stu2;
stu1.id=1;
stu2.id=2;

上面定义了一个结构体类型struct Student 和一个结构体类型变量stu1。

所以有两种定义结构体变量的方式:

一种是这就跟在结构体定义的后面(}之后),一种是用 struct  结构体名  结构体变量名。

2、typedef:typedef作为C的一个keyword,在C 和C++ 中都是给一个数据类型定义一个新的名字。这里的数据类型包含基本数据类型(int, char等)和自己定义的数据类型(struct)。

编程中使用typedef,其目的一般有两个。一个是给变量一个easy记且意义明白的新名字。还有一个是简化一些比較复杂的类型声明。

所以有:

typedef struct Student {
    int id;
    string name;
}Student;
Student stu;
stu.id=1;
stu.name="zhangsan";

当中,typedef 给自己定义类型struct Student 起了一个简单的别名:Student

所以Student stu; 就等价于1中的struct Student stu;

3、typedef 定义批量的类型别名

typedef struct Student {
    int id;
    string name;
}Student1,Student2,Student3;

typedef定义了 3 个struct Student 类型的别名

可是假设去掉了typedef,那么在C++中。Student1,Student2,Student3将是3个结构体变量

当然。假设,Student 以后用不着。则能够省略Student,例如以下所看到的功能与3同样。

typedef struct {
    int id;
    string name;
}Student1,Student2,Student3;

二、C++中的struct使用方法

1、

<pre name="code" class="cpp">struct Student {
    int id;
    string name;
}stu;
stu.id = 1;
stu.name="";

定义了一个Student类型的结构体。还声明了Student类型的一个结构体变量stu。

2、typedef

typedef struct Student {
    int id;
    string name;
}stu2;
stu2 s2;
s2.id=1;
s2.name="zhangsan";

上面 typedef 定义了一个结构体类型 stu2,全部要给id赋值,必须先定义一个结构体类型变量,如s2,然后才干s2.id =1;

3、struct 定义批量的结构体变量

struct Student {
   int id=1;
   string name;
}stu1,stu2,stu3;

定义了3个结构体变量 stu1,stu2,stu3

stu1.id =1;

stu2.id =2;

stu3.id =3;

时间: 2024-08-14 11:21:35

struct结构体在c和c++中的差别的相关文章

go struct结构体

struct结构体 用来自定义复杂数据结构 struct里面可以包含多个字段(属性) struct类型可以定义方法,注意和函数的区分 struct类型是值类型 struct类型可以嵌套 Go语言没有class类型,只有struct类型 1.struct 声明 type 标识符 struct { field1 type field2 type } 样例: type Student struct { Name string Age int Score int } 2.struct 中字段访问:和其他

struct结构体自动化构想性能监控

TABLE_NAME不查询其他数据库中的表.外为了以防万一,可以在SQL语句中写表时加上数据库比如SELECT column_name ,TABLE_NAME,TABLE通过查询information_schema库中的tables DeviceType | tinyint(3) unsigned | YES | | NULL | | | VideoInputNum | tinyint(3) unsigned | YES | | NULL | | | VideoCodecCapacity如果结构

结构体和它在链表中的使用

一.结构体 由不同类型的数据组合成一个整体,以便引用,这些组合在一个整体中的数据是互相联系的. 1.1如何声明结构体呢? struct 结构体名  //结构体名字用作结构体类型的标志 {成员列表}; 比如: 1 struct student 2 { 3 int num; //2 4 char name[20]; //20 5 char sex; //1 6 int age; //2 7 float score; //4 8 char addr[30]; //30 9 }; 注意:声明只是指定了一

struct 结构体以及 typedef struct

1.C语言中,结构体的声明如下:首先关键字struct表示接下来是一个结构,后面是一个可选的标记,用来引用该结构的快速标记. struct book { char title[MAXN]; char author[MAXM]; float value; }; 上述结构声明,是"结构设计"的意思.它告诉编译器如何表示数据,而没有让计算机为数据分配空间. 然后声明或定义一个结构体变量时,如下: struct book library; 其中struct 不能省. 综合上述的两个,可以合二为

C#基础知识---struct(结构体)

结构体和类有点类似    我们定义一个类的时候    是class   类名   定义结构体的时候是 struct   结构体名 下面我们来看看结构体的写法  以及一些注意点 struct Point { // public int x=10; 这种写法错误 public int x ; public int y; public int X { get { return x; } set { x = value; } } public void SayHi() { Console.WriteLi

c语言 struct结构体的变量声明加冒号

有些信息在存储时,并不需要占用一个完整的字节,而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1两种状态,用一位二进位即可. 为了节省存储空间,并使处理简便,C语言又提供了一种数据结构,称为"位域"或"位段".所谓"位域"是把一个字节中的二进位划分为几个不同的区域,并说明每个区域的位数.每个域有一个域名,允许在程序中按域名进行操作.这样就可以把几个不同的对象用一个字节的二进制位域来表示. [1]定义: struct 位域结构名 { 位

struct结构体基础知识

/* ============================================================================ Name : TestStruct.c Author : lf Version : Copyright : Your copyright notice Description : struct结构体基础知识 ==================================================================

Swift Struct 结构体

前言 结构体是值类型,并且只有在赋予变量或常量,或者被函数调用时才被赋值. 1.结构体的创建 结构体基本结构 struct 结构体名 { var 或 let 字段名 = 初始化值 var 或 let 字段名: 类型 } struct Student { var age = 0 } var stu = Student() /// struct 结构体关键字 /// Student 结构体名称 /// student() 创建一个结构体变量 1.1 标准定义 结构体的定义 定义结构体字段时可以直接定

C/C++ 学习之旅 - 实战3 - 在Struct结构体中使用函数Function

Coding部分: #include<stdio.h> struct Student{ int ID; char* Name; int Age; void(*toString)(int id,char* name,int age); };//定义包含学生ID.姓名.年龄和输出函数指针的结构体Student void toString(int id,char* name,int age){ printf("myStudent实例变量的ID值:%d\n",id); printf