struct 做另一个struct的成员

本文通过具体的C语言代码示例介绍了结构体之间的嵌套使用及结构体与指针的交互方式。演示了如何利用指针操作结构体成员,并展示了不同类型指针间的转换及其可能带来的风险。

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

1. struct A 变量可以做另一个struct AB  的成员

2. struct A 的指针可以指向 struct AB 的变量

3. struct AB 的指针 也可以指向 struct A的变量,but dangerous

4. 一切都在内存空间,只是指针指向而已。


/*
 * test.h
 *
 *  Created on: Sep 27, 2012
 *      Author: caoj7
 */

#ifndef TEST_H_
#define TEST_H_

struct date{
	int year;
	int month;
	int day;
};

typedef struct date date_t;

struct timer{
	date_t date_member;
	int hour;
	int minute;
	int second;
};

typedef struct timer timer_t;


#endif /* TEST_H_ */

/*
 * test.c
 *
 *  Created on: Sep 27, 2012
 *      Author: caoj7
 */
#include <stdio.h>
#include <stdlib.h>
#include "test.h"

date_t birth_date = {2012, 9, 26};

int main(void){

	timer_t birth_timer = {birth_date, 1,1,1};
	timer_t *birth_timer_ptr = &birth_timer;
	printf("birth_timer is: %d %d %d, %d:%d:%d\n",
			birth_timer_ptr->date_member.year,
			birth_timer_ptr->date_member.month,
			birth_timer_ptr->date_member.day,
			birth_timer_ptr->hour,
			birth_timer_ptr->minute,
			birth_timer_ptr->second
			);

	birth_timer_ptr->date_member.year =  2000;
	printf("not change value in orginal struct, birth_date.year = %d\n", birth_date.year);
	printf("birth_timer is: %d %d %d, %d:%d:%d\n",
				birth_timer_ptr->date_member.year,
				birth_timer_ptr->date_member.month,
				birth_timer_ptr->date_member.day,
				birth_timer_ptr->hour,
				birth_timer_ptr->minute,
				birth_timer_ptr->second
				);
	//let a member pointer point to a whole struct address
	date_t *date_ptr = (date_t *)birth_timer_ptr;
	printf("%d %d %d\n", date_ptr->year, date_ptr->month, date_ptr->day);

	//
	timer_t *test_timer_ptr = (timer_t *)date_ptr;
	printf("%d %d %d, %d:%d:%d\n",
			test_timer_ptr->date_member.year,
			test_timer_ptr->date_member.month,
			test_timer_ptr->date_member.day,
			test_timer_ptr->hour,
			test_timer_ptr->minute,
			test_timer_ptr->second);

	test_timer_ptr = (timer_t *)&birth_date;
	printf("%d %d %d, %d:%d:%d\n",
				test_timer_ptr->date_member.year,
				test_timer_ptr->date_member.month,
				test_timer_ptr->date_member.day,
				test_timer_ptr->hour,
				test_timer_ptr->minute,
				test_timer_ptr->second);

	return EXIT_SUCCESS;
}


SystemVerilog是一种用于电子系统级设计和验证的语言,它在Verilog的基础上增加了面向对象编程的特性,其中包括结构体(struct)的概念。在SystemVerilog中,可以使用约束(constraint)来随机化结构体中的成员。 假设我们有一个名为`my_struct`的结构体,其中包含几个不同的成员,我们想要随机化其中一个名为`member`的成员,可以按照以下步骤进行: 1. 定义结构体`my_struct`,包含多个成员,例如: ```systemverilog struct packed { logic [7:0] member; logic [3:0] other_member; // ... 可能还有其他成员 ... } my_struct; ``` 2. 使用约束来定义`member`的随机化规则。例如,我们希望`member`的值在0到255之间随机,可以这样定义约束: ```systemverilog constraint valid_member { member inside {[0:255]}; } ``` 3. 在测试环境中,创建一个`my_struct`类型的变量,并应用约束进行随机化: ```systemverilog module testbench; my_struct s; initial begin // 应用约束 constraint c { valid_member; } // 随机化 assert(randomize(s) with {c;}); // 打印随机化后的结果 $display("Randomized struct member: %d", s.member); end endmodule ``` 在上述代码中,我们定义一个`my_struct`类型的变量`s`,并且在`initial`块中首先声明了一个约束`c`,其中包含了我们想要应用的规则`valid_member`。然后使用`randomize`函数随机化`s`,并指定使用之前定义的约束`c`。最后,通过`$display`系统任务输出随机化后的`member`值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值