this is the definition and declaration of the structure
typedef struct ST_accountsDB_t
{
float balance;
uint8_t primaryAccountNumber[20];
}ST_accountsDB_t;
ST_accountsDB_t accountsDB[255];
then I started filling it
accountsDB[0].balance = 20000;
accountsDB[0].primaryAccountNumber[20] = "1234567891234567";
accountsDB[1].balance = 50000;
accountsDB[1].primaryAccountNumber[20] = "9872653461728839";
accountsDB[2].balance = 40000;
accountsDB[2].primaryAccountNumber[20] = "6873645738467382";
accountsDB[3].balance = 28000;
accountsDB[3].primaryAccountNumber[20] = "3872634547838276";
accountsDB[4].balance = 3000;
accountsDB[4].primaryAccountNumber[20] = "1283764957873637";
accountsDB[5].balance = 100000;
accountsDB[5].primaryAccountNumber[20] = "3485793029384758";
accountsDB[6].balance = 30000;
accountsDB[6].primaryAccountNumber[20] = "8746330283748393";
this is the normal way to fill a string right ? why isn't it working ? am I missing something ?
CodePudding user response:
You want the compiler to fill-in a character array from a string. Don't make it difficult for everyone. Copy/paste/adapt is NOT the way to develop code.
typedef struct ST_accountsDB_t {
float balance;
char primaryAccountNumber[20]; // NB char, not uint8_t
} ST_accountsDB_t;
ST_accountsDB_t accountsDB[] = {
{ 20000.0, "1234567891234567", },
{ 50000.0, "9872653461728839", },
{ 40000.0, "6873645738467382", },
{ 28000.0, "3872634547838276", },
{ 3000.0, "1283764957873637", },
{ 100000.0, "3485793029384758", },
{ 30000.0, "8746330283748393", },
};
And, using the dimension 255 suggests you've spent the time counting... The compiler counts more accurately than most humans.
CodePudding user response:
You can initialize it when you declare it, using an initialization list.
ST_accountsDB_t accountsDB[255] = {
{20000, "1234567891234567"},
{50000, "9872653461728839"},
...
};
If you want to fill it in after declaring it, use strcpy()
to copy strings.
strcpy(accountsDB[0].primaryAccountNumber, "1234567891234567");
Even though uint8_t
is equivalent to unsigned char
, it's conventional to use char
arrays to declare strings.
typedef struct ST_accountsDB_t
{
float balance;
unsigned char primaryAccountNumber[20];
}ST_accountsDB_t;