Re: storing values in typedef struct error.

Tom Collins <tom-lnEA/wrDJtNWk0Htik3J/[email protected]>
Newsgroups gmane.comp.hardware.rabbit-semiconductor
Message-ID <[email protected]>
You're not null-terminating your strings.

If mc1 contains '1', '2', '3', '4', '5', '6', '7', '8' and mc2 contains the same thing, there's no null ('\0') between them to make them separate strings.

-Tom


On May 22, 2013, at 8:45 AM, racqueldesign wrote:

> Hi,
> 
> Whenever I store in string in a struct it store correctly; however, if try to store another string in another variable it also store correctly but the first storage variable now contain both strings. 
> 
> If I continue storing strings to other variable in the struct previous variables are modified(added) to have this new string. For example:
> 
> typedef struct{
> unsigned char mc1[8];
> unsigned char mc2[8];
> unsigned char mc3[8];
> unsigned char mc4[8];
> unsigned char mc5[8];
> unsigned char mc6[8];
> unsigned char mc7[8];
> unsigned char mc8[8];
> unsigned char mc9[8];
> unsigned char mc10[8];
> unsigned char pmcbindid[8];
> unsigned char pmcdump[63];
> unsigned char pmckey[16];
> unsigned char pmcen;
> 
> }mc_t;
> 
> mc_t device;
> 
> main() {
> 
> unsigned char InBuffer[255];
> unsigned char *src;
> signed char msg_index;
> 
> ...
> src = InBuffer + Tokens[msg_index+4].start;
> 
> _f_strncpy(device.mc1,src, 8);
> _f_strncpy(device.mc2,src, 8);
> _f_strncpy(device.mc3,src, 8);
> _f_strncpy(device.mc4,src, 8);
> _f_strncpy(device.mc5,src, 8);
> _f_strncpy(device.mc6,src, 8);
> _f_strncpy(device.mc7,src, 8);
> _f_strncpy(device.mc8,src, 8);
> _f_strncpy(device.mc9,src, 8);
> _f_strncpy(device.mc10,src, 8);
> 
> //test only
> printf("\n mc1 = %s \n mc2 = %s \n mc3 = %s \n mc4 = %s \n mc5 = %s \n mc6 = %s \n mc7 = %s \n mc8 = %s \n mc9 = %s \n mc10 = %s\n",device.mc1,device.mc2,device.mc3,device.mc4,device.mc5,device.mc6,device.mc7,device.mc8,device.mc9,device.mc10);
> }
> 
> Output after each storage:
> 1st.
> ==============
> mc1 = 12345678
> mc2 =
> mc3 = 
> mc4 =
> mc5 = 
> mc6 =
> mc7 = 
> mc8 =
> mc9 = 
> mc10 =
> 
> 2nd
> ==========================
> mc1 = 1234567812345678
> mc2 = 12345678
> mc3 = 
> mc4 =
> mc5 = 
> mc6 =
> mc7 = 
> mc8 =
> mc9 = 
> mc10 =
> 
> 3rd
> ==========================
> mc1 = 123456781234567812345678
> mc2 = 1234567812345678
> mc3 = 12345678
> mc4 =
> mc5 = 
> mc6 =
> mc7 = 
> mc8 =
> mc9 = 
> mc10 =
> 
> 4th
> ==========================
> mc1 = 12345678123456781234567812345678
> mc2 = 123456781234567812345678
> mc3 = 1234567812345678
> mc4 = 12345678
> mc5 = 
> mc6 =
> mc7 = 
> mc8 =
> mc9 = 
> mc10 =
> 
> An so on. Why are current strings adding to previous variables? Also if I was to store another string at device.mc2, device.mc3 is now NULL.
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.