Reputation: 327
Creating an uint8_t "string" works fine for me:
uint8_t myString[] = "Foo";
However when I try to declare it in a struct, it does not work:
#ifndef PLAYER_H_
#define PLAYER_H_
#include <stdint.h>
typedef struct{
uint32_t id;
uint8_t name[];
uint16_t life;
uint16_t mana;
uint16_t fist;
uint16_t shielding;
} player;
#endif // PLAYER_H_
I get a compilation message saying: Field has incomplete type uint8_t[].
What am I missing here guys?
Upvotes: 2
Views: 4036
Reputation: 92211
You have to give name
a size inside the string. Otherwise the compiler doesn't know how large the struct is.
When declaring a variable like
uint8_t myString[] = "Foo";
the compiler can figure the size out from the size of "Foo"
, but if you declare an array without an initial value, you have to tell the size.
(The comments question if a uint8_t
array can really be initialized with a string. On systems where char
is unsigned, uint8_t
could be a typedef for char
.)
Upvotes: 2
Reputation: 16607
uint8_t name[];
You have not mentioned size here , either mention size of name
. You cannot create array of unknown size in somewhere middle of your structure.
uint8_t name[50]; // this could do
And you could initialize it like this -
player p1={.name="hello"};
Or use memcpy
.
Upvotes: 2
Reputation: 12402
if you want to have an array of indeterminate size in a struct it must be the last element of the struct, and when allocating memory for the struct you must allocate extra for the array contents.
Upvotes: 1