结构体总结

举报
白茶加冰 发表于 2023/07/30 23:16:30 2023/07/30
【摘要】 ​ 目录1.普通结构体2.定义结构体并同时建立变量3.匿名结构体 4.typedef重命名5.typedef省略结构体名字6.结构体数组 7.结构体指针8.结构体嵌套 9.结构体链表(头插法) 10.结构体中的函数指针 11.结构体的初始化列表 12.结构的的构造函数13.结构体重载运算符1.普通结构体struct   name(结构体名){        结构体成员};#include <...

 目录

1.普通结构体

2.定义结构体并同时建立变量

3.匿名结构体

 4.typedef重命名

5.typedef省略结构体名字

6.结构体数组

 7.结构体指针

8.结构体嵌套

 9.结构体链表(头插法)

 10.结构体中的函数指针

 11.结构体的初始化列表

 12.结构的的构造函数

13.结构体重载运算符



1.普通结构体

struct   name(结构体名){

        结构体成员

};

#include <stdio.h>
struct node
{
    int a;
    char *b;
};

int main(int argc, char const *argv[])
{
    struct node n;
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

2.定义结构体并同时建立变量

struct   name(结构体名){

        结构体成员

}实例变量;

#include <stdio.h>
struct node
{
    int a;
    char *b;
}n,nn;

int main(int argc, char const *argv[])
{
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

3.匿名结构体

struct{

        结构体成员

}实例变量;

#include <stdio.h>
struct
{
    int a;
    char *b;
}n,nn;

int main(int argc, char const *argv[])
{
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

 4.typedef重命名

typedef  struct   name1结构体名){

        结构体成员

}name2(重新定义的名字);

name2=struct name1

#include <stdio.h>
typedef struct node
{
    int a;
    char *b;
}node;

int main(int argc, char const *argv[])
{
    node n;
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

5.typedef省略结构体名字

typedef  struct {

        结构体成员

}name2(重新定义的名字);


#include <stdio.h>
typedef struct
{
    int a;
    char *b;
}node;

int main(int argc, char const *argv[])
{
    node n;
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

6.结构体数组

和创建普通数组一样创建结构体数组即可

#include <stdio.h>
typedef struct node
{
    int a;
    char *b;
}node;

int main(int argc, char const *argv[])
{
    node n[10];
    for(int i=0;i<10;i++){
        n[i].a=i*i;
        n[i].b="zbcdef";
    }
     for(int i=0;i<10;i++){
        printf("%d\t%s\n",n[i].a,n[i].b);
    }
    return 0;
}

编辑

 7.结构体指针

extern void *malloc(unsigned int num_bytes);

首先定义一个结构体指针,但此时只是分配了一个地址,结构体成员变量并没有分配空间,因此使用malloc函数动态分配地址, 返回值是一个void*,因此要强转成你要的类型指针。

结构体成员变量赋值:

假如n是一个结构体指针,*n就是这个实际结构体,  (*n).成员名   就可以给结构体成员赋值或者更改,此外C语言为了简便,单独规定了结构体指针的定义方式  n->成员名。两种都是可以的;

#include <stdio.h>
typedef struct
{
    int a;
    char*  b;
}node;

int main(int argc, char const *argv[])
{
    node *n=(node*)malloc(sizeof(node));
    (*n).b="abcdefg";
    (*n).a=100;
    printf("%s\n%d\n",(*n).b,(*n).a);
    return 0;
}

#include <stdio.h>
typedef struct
{
    int a;
    char*  b;
}node;

int main(int argc, char const *argv[])
{
    node *n=(node*)malloc(sizeof(node));
    n->b="abcdefg";
    n->a=100;
    printf("%s\n%d\n",n->b,n->a);
    return 0;
}

8.结构体嵌套

结构体的成员变量当然可以包含结构体了

#include <stdio.h>
typedef struct student
{
   char* name;
   int age;
   int chengji;
}student;

typedef struct school_class
{
    student std[3];
    char* t_name;
    int t_age;
    
}school_class;

int main(int argc, char const *argv[])
{
    school_class *n=(school_class*)malloc(sizeof(school_class));
    n->t_name="曹老师";
    n->t_age=21;
    n->std[0].name="小明";
    n->std[0].age=18;
    n->std[0].chengji=500;
    n->std[1].name="小张";
    n->std[1].age=17;
    n->std[1].chengji=600;
    n->std[2].name="小红";
    n->std[2].age=18;
    n->std[2].chengji=700;
    printf("老师信息:\n%s\t%d\n",n->t_name,n->t_age);
    for(int i=0;i<3;i++){
        printf("学生%d信息:\n",i);
        printf("%s\t%d\t%d\n",n->std[i].name,n->std[i].age,n->std[i].chengji);
    }
    return 0;
}

编辑

 9.结构体链表(头插法)

链表就是结构体的指针指向下一个结构体,其存储不连续,是分布式的,因此读取速度相对数组来说慢的多。定义一个结构体,其结构体成员变量中包括本身的一个结构体指针,因此可以给该结构体变量赋值,赋的值又是一个结构体指针,里面又有一个结构体指针类型的结构体成员,以此类推产生链表。C++的STL库有list库,使用十分方便;

#include"stdlib.h"
#include"stdio.h"
static int wei=1;
typedef struct Node 
{
	char data[15]; 
	struct Node* next;
}node;
node* head;
void Insert()
{
	node* p=(node*)malloc(sizeof(node));
	char buff[10];
	sprintf(buff,"我是第%d个元素!",wei);
	int i=0;
    while(buff[i]!='\0'){
    	p->data[i]=buff[i];
    	i++;
	}
	p->data[i]='\0';
    p->next=head;
    wei++;
    head=p;
 }
 
void Print()
{
	node* p=head;
	while(p!=NULL)
	{
		printf("%s\n",p->data);
		p=p->next;
	}
}//自定义打印函数(Print)
 
int main()
{
	head=NULL;
	for(int i=0;i<5;i++)
	{
	    Insert();
	}
	Print();
}

 编辑

 10.结构体中的函数指针

#include <stdio.h>
typedef struct Node{
	int x;
	int y;
	int (*cheng)(int x,int y);
}node;

int cheng_1(int x,int y){
	return x*y;
}
int main(){
	node m;
	m.x=10;
	m.y=9;
	m.cheng=cheng_1;
	printf("%d\n",m.cheng(m.x,m.y));
	return 0;
}

 编辑

 11.结构体的初始化列表

#include <iostream>
using namespace std;
struct node{
	int age;
	string name;
	int weight;
	int height;
	node(string name_1="曹XX",int age_1=18,int weight_1=120,int height_1=180){
		name=name_1;
		age=age_1;
		weight=weight_1;
		height=height_1; 
	}
};
int main(){
	node cao("曹仙人",22,123,185);
	cout<<cao.name<<' '<<cao.age<<' '<<cao.height<<' '<<cao.weight<<endl; 
	return 0;
}

编辑

 12.结构的的构造函数

#include <iostream>
using namespace std;
struct node{
	string name;
	int age;
	int weight;
	int height;
	node():name("曹仙人"),age(22),weight(120),height(190){
		cout<<"构造函数"<<endl; 
	} 
	~node(){
		cout<<"析构函数"<<endl; 
	}
};
int main(){
	node t;
	cout<<t.name<<' '<<t.age<<' '<<t.weight<<' '<<t.height<<endl;
	return 0;
}

 编辑

13.结构体重载运算符

#include <iostream>
#include <algorithm>
using namespace std;
struct node{
	int x;
	int y;
	bool operator<(const node &rhs)const {
		if(x==rhs.x){
			return y>rhs.y;
		}else{
			return x>rhs.x;
		}
	}
};
int main(){
	node a[10];
	for(int i=0,j=10;i<10;i++,j--){
		a[i].x=i;
		a[i].y=j;
	}
	cout<<"排序前"<<endl; 
	cout<<"----------------------"<<endl;
	for(int i=0;i<10;i++){
		cout<<"x:"<<a[i].x<<' '<<"y:"<<a[i].y<<endl;
	}
	sort(a,a+10);
	cout<<"排序后"<<endl; 
	cout<<"----------------------"<<endl;
	for(int i=0;i<10;i++){
		cout<<"x:"<<a[i].x<<' '<<"y:"<<a[i].y<<endl;
	}
	return 0;
} 

 编辑

14结构体对齐规则

一、结构体对齐规则

1、第一个成员在与结构体偏移量为0的地址处;

2、其他成员变量要与自身类型的整数倍地址处对齐;

3、结构体总大小为要与 “处理器字节数与成员类型所占字节数最大的最小值” 的整数倍对齐;

4、如果出现嵌套情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对\n齐数(含嵌套结构体的对齐数)的整数倍。

5、#pragma pack(n) 可以用来控制默认对齐数的大小

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。