c - 'variable' 的存储大小未知

标签 c struct

我有一个关于“a 的存储大小未知”的问题。我检查了计算器并找到了一些答案。但是,这些解决方案不起作用,或者我不能很好地使用这些答案。

请帮助我解决我的问题。 感谢您的帮助,祝您有愉快的一天。

main.c 文件

#include <avr/io.h>
#include <PORT.h>

int main(void)
{

    // Insert code
    PORT a;

    while(1)
    ;

    return 0;
}

PORT.h文件

#ifndef PORT_H_INCLUDED
#define PORT_H_INCLUDED

#include <config.h>

    typedef enum PORT_TYPE{
    // All port in ATmega328P
        PORT_B,
        PORT_C,
        PORT_D
    }PORT_TYPE;

    typedef enum PORT_PIN{
    // All pins in ATmega328P
        PIN_0,
        PIN_1,
        PIN_2,
        PIN_3,
        PIN_4,
        PIN_5,
        PIN_6,
        PIN_7
    }PORT_PIN;

    typedef struct PORT PORT;

    void PORT_init(PORT * const me,
                   void (* setDirectionFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* setStatusFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* enablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* disablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* notifyPinChangeFunction)(PORT * const me, PORT_PIN pinNumber),
                   unsigned char (*readPINFunction)(PORT * const me, PORT_PIN pinNumber),
                   unsigned char (*readPortFunction)(PORT * const me));

    void PORT_setDirection(PORT * const me, PORT_PIN pinNumber);
    void PORT_setStatus(PORT * const me, PORT_PIN pinNumber);
    void PORT_enablePullResistor(PORT * const me, PORT_PIN pinNumber);
    void PORT_disablePullResistor(PORT * const me, PORT_PIN pinNumber);
    void PORT_notifyPinChange(PORT * const me, PORT_PIN pinNumber);
    unsigned char PORT_readPIN(PORT * const me, PORT_PIN pinNumber);
    unsigned char PORT_readPort(PORT * const me);

    PORT * PORT_create(PORT_TYPE whichPort);
    void PORT_destroy(PORT * const me);



#endif // PORT_H_INCLUDED

PORT.c文件

#include <avr/io.h>
#include <PORT.h>

#define ADDR_PORTB (0x0023)
#define ADDR_PORTC (0x0026)
#define ADDR_PORTD (0x0029)

typedef volatile struct{
// it is used for manipulating registers.
    unsigned char pin;
    unsigned char ddr;
    unsigned char port;
}PORT_hw;

struct PORT{
    unsigned char changePIN;
    PORT_hw volatile *p_hw;
    void (* setDirection)(PORT * const me, PORT_PIN pinNumber);
    void (* setStatus)(PORT * const me, PORT_PIN pinNumber);
    void (* enablePullResistor)(PORT * const me, PORT_PIN pinNumber);
    void (* disablePullResistor)(PORT * const me, PORT_PIN pinNumber);
    void (* notifyPinChange)(PORT * const me, PORT_PIN pinNumber);
    unsigned char (*readPIN)(PORT * const me, PORT_PIN pinNumber);
    unsigned char (*readPort)(PORT * const me);
};

static PORT g_PORT[3];
g_PORT[0].p_hw = (PORT_hw volatile *)ADDR_PORTB;
g_PORT[1].p_hw = (PORT_hw volatile *)ADDR_PORTC;
g_PORT[2].p_hw = (PORT_hw volatile *)ADDR_PORTD;

void PORT_init(PORT * const me,
                   void (* setDirectionFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* setStatusFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* enablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* disablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber),
                   void (* notifyPinChangeFunction)(PORT * const me, PORT_PIN pinNumber),
                   unsigned char (*readPINFunction)(PORT * const me, PORT_PIN pinNumber),
                   unsigned char (*readPortFunction)(PORT * const me)){
    me->changePIN = 0x00;
    me->pinStatus = 0x00;
    me->setDirection = setDirectionFunction;
    me->setStatus = setStatusFunction;
    me->enablePullResistor = enablePullResistorFunction;
    me->disablePullResistor = disablePullResistorFunction;
    me->notifyPinChange = notifyPinChangeFunction;
    me->readPIN = readPINFunction;
    me->readPort = readPortFunction;
}

void PORT_setDirection(PORT * const me, PORT_PIN pinNumber){}
void PORT_setStatus(PORT * const me, PORT_PIN pinNumber){}
void PORT_enablePullResistor(PORT * const me, PORT_PIN pinNumber){}
void PORT_disablePullResistor(PORT * const me, PORT_PIN pinNumber){}
void PORT_notifyPinChange(PORT * const me, PORT_PIN pinNumber){}
unsigned char PORT_readPIN(PORT * const me, PORT_PIN pinNumber){}
unsigned char PORT_readPort(PORT * const me){}


PORT * PORT_create(PORT_TYPE whichPort){
    PORT *p_PORT = &(g_PORT[whichPort]);
    PORT_init(p_PORT, PORT_setDirection, PORT_setStatus, PORT_enablePullResistor, PORT_disablePullResistor, PORT_notifyPinChange, PORT_readPIN, PORT_readPort);
    return p_PORT;
}
void PORT_destroy(PORT * const me){}

最佳答案

PORT.h 头文件声明 struct PORT 存在,但没有定义它:

typedef struct PORT PORT;

真正的定义在PORT.c中,在main.c中是看不到的。

因此,您无法创建 PORT 类型的变量。但是,您可以创建 PORT * 类型之一。由于定义未知,因此称为不透明指针

根据 PORT.h 中定义的函数判断,您可以使用 PORT_create 函数返回 PORT *,随后可以将其传递给其他函数。

关于c - 'variable' 的存储大小未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42696713/

相关文章:

c - 不知何故,1 + 48 输出 17,然后是 1

c - 如何复制动态分配的结构数组?

c - 使用数组索引将值直接存储到 C 结构中

c - C中结构内数组的初始化

c - 将指针从 main() 移动到第一个可执行函数

C编程-关于while循环再次提示用户问题

c - 如何获得二维数组的总和?

无法释放 C 中的一些内存

c - 动态调整大小的结构体 - 艰难地学习 C

c - 在 C 中释放数组中结构体字段的内存