I want to copy each element of an array of strings to another array of strings, but when I copy one string whole array is changed to that string. Following is the code set.
This is the line of code having issue:
strcpy(MsgList[i].ga_data, a_Database[i].ga_data);
when I check the contents of a_Database[i].ga_data, it is as below
"1240,message 7:War of the worlds"
"1238,message 5:Life of this world"
"1236,message 3:world is not enough"
"1235,message 2:What a world!"
So instead of populating the same in MsgList[i].ga_data
, it turns out to be just "1235,message 2:What a world!" 4 times, which is the last element copied.
typedef enum
{
ACK,
NACK,
DELETE
}eMsgStatus_t;
typedef struct _Message
{
eMsgStatus_t status;
char *ga_data;
uint16_t time;
}Message_t;
Message_t MessageList[8]={
{ACK,"1234,message 1:Hello world",1000},
{NACK,"1235,message 2:What a world!",1011},
{NACK,"1236,message 3:world is not enough",1022},
{ACK,"1237,messsge 4:Cruel world",1033},
{NACK,"1238,message 5:Life of this world",1044},
{ACK,"1239,message 6:Around the world in 80 days",1055},
{NACK,"1240,message 7:War of the worlds",1066},
{ACK,"1241,message 8:End of World",1077}
};
Message_t a_Database[20];
Message_t MsgList[20]= {0};
int main()
{
for (i = 0; i < idx /* total unread message */; ++i)
{
strcpy(MsgList[i].ga_data, a_Database[i].ga_data);
MsgList[i].ga_data[14] = '\0';
}
}
uint8_t GetMessages (Message_t *pg_Message)
{
char i;uint8_t idx = 0;
for (i = 0; i < 8 /* total message */; ++i)
{
if(NACK == MessageList[i].status)
{
pg_Message[idx].status = MessageList[i].status;
pg_Message[idx].ga_data = MessageList[i].ga_data;
pg_Message[idx].time = MessageList[i].time;
idx++;
}
}
return idx;
}