访问结构中的指针(变量)结构的成员 [英] Accessing a member of a pointer (variable) struct within a struct

查看:491
本文介绍了访问结构中的指针(变量)结构的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很重要:我正在研究一个C函数,该函数配置一个包含许多成员的结构,例如* BASE(指向结构的指针),ID,MODE;但是BASE是一个结构,根据接口的不同,可以将其定义为结构a",结构b","...".这是结构(和一些示例)声明:

Here's the deal: I am working on a C function that configures a struct that contains many members, as the *BASE (Pointer to a Struct),ID,MODE; but the BASE is a struct that might be defined as (say) "struct a", "struct b", "...", depending of the interface. This is the struct (and some examples) declaration:

typedef unsigned int u32_t;
typedef unsigned char u8_t;
typedef struct _interface_t{
    u32_t *BASE;
    u8_t ID;
    u32_t MODE;
} interface_t;

interface_t USART0_DEV  = {AT91C_BASE_US0, AT91C_ID_US0, 0}; // <-- Here we have a AT91C_BASE_US0 struct as *BASE
interface_t USART1_DEV  = {AT91C_BASE_US1, AT91C_ID_US1, 0};
interface_t TC0_DEV     = {AT91C_BASE_TC0, AT91C_ID_TC0, 0}; // <-- Here we have a AT91C_BASE_TC0 struct as *BASE
interface_t TC1_DEV     = {AT91C_BASE_TC1, AT91C_ID_TC1, 0};
interface_t TC2_DEV     = {AT91C_BASE_TC2, AT91C_ID_TC2, 0};
interface_t TWI_DEV     = {AT91C_BASE_TWI, AT91C_ID_TWI, 0}; // <-- Here we have a AT91C_BASE_TWI struct as *BASE

如您所见,我使用了u32_t而不是该结构,因为我声明了一个指向该结构的指针.后来我将函数定义为:

As you see I used u32_t instead of the struct because I'm declaring a pointer to that struct. Later I have my function defined as:

unsigned char ConfigureDevice(interface_t *Interface, u32_t config, u32_t Speed, u32_t IRQ_Trigger, void (*Interface_irq_handler)(void)){
    ...
    Interface->BASE->US_IER = IRQ_Trigger; //Note: US_IER Must receive a vector to a function
    ...
}

但是我收到错误消息"US_IER无法解决". 因此,我尝试使用AT91S_USART InterfaceBASE = Interface->BASE; InterfaceBASE->US_IER = IRQ_Trigger;而不是Interface->BASE->US_IER = IRQ_Trigger;并没有出现错误.但是我不能使用它,因为我并不总是知道我正在处理哪种接口(直到我读完ID成员).

But I get error 'US_IER could not be resolved'. So I tried AT91S_USART InterfaceBASE = Interface->BASE; InterfaceBASE->US_IER = IRQ_Trigger; instead of Interface->BASE->US_IER = IRQ_Trigger; and got no error. But I can't use this, because I don't always know what kind of interface I'm handling (until I read ID member).

它甚至变得更糟:当我尝试编译时,在每个interface_t定义中的u32_t typedef和initialization from incompatible pointer typenear initialization for USART0_DEV.BASE警告中不断出现conflicting type qualifiers for 'u32_t'错误.

It gets even worst: When I try to compile I keep getting conflicting type qualifiers for 'u32_t' error in the u32_t typedef and initialization from incompatible pointer type and near initialization for USART0_DEV.BASE warnings in each of the interface_t definitions.

我在论坛中进行了搜索,但无法弄清楚我的错误.这些帮助:

I searched in the forums but I couldn't figure my error(s) out. These ones kinda helped:

  • creating struct within struct
  • Defining struct within typedef struct

我的代码有什么问题? 如何使用Interface->BASE->US_IER

What's wrong with my code? What can I do to use something like Interface->BASE->US_IER

您好!现在,我得到了:

Hi!, so now this is what I got:

typedef struct _Interface_t{
    struct PERIPH_BASE * BASE;
    u32_t ID;
    u32_t MODE;
Interface_t;

Interface_t USART0_DEV  = {(struct PERIPH_BASE *)AT91C_BASE_US0, AT91C_ID_US0, 0};
...
unsigned char ConfigureDevice(Interface_t *Interface, u32_t config, u32_t Speed, u32_t IRQ_Trigger,...
...
((AT91S_SYS)Interface->BASE)->AIC_IECR = IRQ_Trigger; //No problems marked by Eclipse Code Editor
....

}

看起来不错,但是编译时出现cast to union type from type not present in union错误...

Looks Fine, but when compiling I get cast to union type from type not present in union Error...

推荐答案

BASE是类型为u32_t pointer的变量,它试图访问成员US_IER,而该成员在u32_t pointer中并不是真正可用的.但是您可能无法实现将其定义为u32_t pointer而不是structure pointer的期望,因为编译器会期望如果通过结构指针访问该成员,则应该已经定义了该成员.

BASE is a variable of type u32_t pointer and trying to access a member US_IER which is not really available in u32_t pointer. But your expectation of having this defined as a u32_t pointer instead structure pointer may not be possible as the compiler will expect a member should have been defined if it is accessed via a struct pointer.

但是对于您的情况(即具有128种不同的接口类型),请定义具有这3个必需成员的结构类型(在所有128个接口中通用)US_IER, US_IAM, US_IDM,并在该结构内定义具有所需可能成员的并集,然后尝试访问这些物品带有开关盒或其他物品.

But for your case(ie, having 128 different interface type), define a structure type with these 3 required members(common in all 128 interfaces) US_IER, US_IAM, US_IDM and define an union within that structure with required possible members and try accessing these items with a switch case or if else.

这篇关于访问结构中的指针(变量)结构的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆