结构的声明

本文详细介绍了C++中结构体的定义与使用方法,包括基本结构体与复合型结构体的定义,以及如何通过typedef创建新的类型。同时,还探讨了结构体内部引用自身或其他结构体的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在声明结构时,必须列出它包含的所有成员,这个列表包含每个成员的类型和名字。
struct tag { membler-liset }  varibale-lists;
 
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
	struct SIMPLE {
		int a;
		char b;
		float c;
	};
	struct SIMPLE x;
	struct SIMPLE y[20],*z;  //现在它们是同一种类型了。

	z = &x;
}


若果没有标签字段,则会被当做截然不同的字段。

 

或者,使用 typedef 创建一种新的类型。

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
	typedef struct {
		int a;
		char b;
		float c;
	} SIMPLE;
	
	SIMPLE x;	
	SIMPLE y[20], *z;
}


 

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
	struct SIMPLE {
		int a;
		char b;
		float c;
	};
	

	struct COMPLEX  {
		float	f;
		int	a[20];
		long	*lp;
		struct	SIMPLE	s;
		struct SIMPLE	sa[10];
		struct	SIMPLE	*sp;
	};
	
	struct COMPLEX comp;
	comp.sa[4].c;

	struct SELF_REF1 {
		int	a;
		struct		SELF_REF1 *b;
		int	c;
	};
	
	typedef struct	SELF_REF3_TAT {
		int	a;
		struct SELF_REF3_TAG	*b;
		int	c;
	} SELF_REF3;

	struct B;
	struct A {
		struct B  *x;  // 必须以指针的形式存在。
	};
	
	struct B {
		struct A y;
	};
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值