0

I am having trouble declaring an array of structs prior to populating them with data.

My struct looks like this:

typedef struct {
  uint8_t * p_data;     ///< Pointer to the buffer holding the data.
  uint8_t   length;     ///< Number of bytes to transfer.
  uint8_t   operation;  ///< Device address combined with transfer direction.
  uint8_t   flags;      ///< Transfer flags (see @ref NRF_TWI_MNGR_NO_STOP).
} nrf_twi_mngr_transfer_t;

And in my code I am trying to declare the array like this:

struct nrf_twi_mngr_transfer_t start_read_transfer[10];

However I get a compile error:

array type has incomplete element type 'struct nrf_twi_mngr_transfer_t'

I have searched around as I thought should be a common thing, but I can't figure out what I am doing wrong. Maybe because one of the elements is a pointer? But that pointer should be a fixed size right?

Many thanks

3
  • 5
    nrf_twi_mngr_transfer_t start_read_transfer[10]; - i.e. no need for struct there, you are already typedefing it. Commented Feb 28, 2019 at 21:43
  • 3
    There is no struct nrf_twi_mngr_transfer_t type defined. Your typedef is defining type named nrf_twi_mngr_transfer_t, which happens to be a struct. Commented Feb 28, 2019 at 21:44
  • Drop the strict keyword when you use the typedef name. Commented Feb 28, 2019 at 21:45

1 Answer 1

5

It looks like some explanations are in order. This code

typedef struct {
    //...
} nrf_twi_mngr_transfer_t;

Already defines a type which can be used directly. In contrast,

struct nrf_twi_mngr_transfer_struct {
    //...
};

Would define a struct name, and to access it you'd need to indicate that you are referring to a struct.

As a result, given two definitions above, you should define your arrays differently:

nrf_twi_mngr_transfer_t arr[10]; // if using typedef
struct nrf_twi_mngr_transfer_struct arr2[10]; // if using struct with no typedef

And just in case you are wondering,

struct {
    //...
} nrf_twi_mngr_transfer_obj;

Defines an object of anonymous struct type.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. i was overthinking it.. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.