同一结构体变量在不同的.C文件中使用

struct.h文件

#ifndef __STRUCT_H
#define __STRUCT_H

#include "stm32f10x.h"                  // Device header

typedef struct
{
	uint16_t num1;
	uint16_t num2;
	uint16_t num3;
}num;

extern num num_Test;



#endif

 struct.c

#include "STRUCT.H"
#include "main.h"

num num_Test;

void test_Function()
{
	num_Test.num1 =2;
	r_t_Arr.RecveLen = 1;
}

main.h

#ifndef __MAIN_H
#define __MAIN_H

#include "stm32f10x.h"                  // Device header

#define DATA_SIZE 256
typedef struct
{
	uint8_t test_Arr_Struct[DATA_SIZE]; 
	uint8_t RecveLen;
    uint8_t transLen;
	uint16_t rAddr;
	uint16_t wAddr;
}test_Struct;

extern 	test_Struct r_t_Arr;



#endif

main.c

#include "stm32f10x.h"                  // Device header
#include "main.h"
#include "STRUCT.H"

test_Struct r_t_Arr;

int main(void)
{
	while (1)
	{
		num_Test.num1++;
		r_t_Arr.RecveLen++;
	}
}

仅需在1.h文件中对结构体定义,再extern声明为全局变量,再在1.c文件中实例化结构体,即可在2.c(需要#include 1.h)文件中直接使用此结构体无需再次实例化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值