'变量'的stroage大小尚不清楚

时间:2017-03-09 13:30:05

标签: c struct

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

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

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){}

1 个答案:

答案 0 :(得分:2)

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

typedef struct PORT PORT;

实际定义在PORT.c中,在main.c中不可见。

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

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