Search code examples
cavr

How to initialize arrays inside a structure


I am trying to initialize this Kind of structure but it just won't work. Any ideas what the problem is over here?

    #include <stdint.h>
    #define txBufLen 3

    struct {
        uint8_t Buf[txBufLen];
        uint16_t out;
        uint16_t len;
    }txBuf;


struct txBuf a = {{1, 2, 3}, 5, 3 };

Solution

  • struct {
        uint8_t Buf[txBufLen];
        uint16_t out;
        uint16_t len;
    }txBuf;
    

    This defined an untagged struct type, and immediately created a global variable of that type.

    You need to change the definition to this:

    struct txBuf {
        uint8_t Buf[txBufLen];
        uint16_t out;
        uint16_t len;
    };