By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Struct C

P: 2
Hi,

There is structure defined "a" and the same is assigned in str1 ..

iam getting the junk value in end of the string...please is there someone to help me or advice the same.

Please find the code below..
but the Output of str1: "12345678901234567890"




struct a
{
char str1[20];
short len;
};

void CString1Dlg::OnButton1()
{
a b ;
strcpy(b.str1,"12345678901234567890");
b.len = 1;
}

Output : "12345678901234567890"
Aug 14 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
Hi,

There is structure defined "a" and the same is assigned in str1 ..

iam getting the junk value in end of the string...please is there someone to help me or advice the same.

Please find the code below..
but the Output of str1: "12345678901234567890"




struct a
{
char str1[20];
short len;
};

void CString1Dlg::OnButton1()
{
a b ;
strcpy(b.str1,"12345678901234567890");
b.len = 1;
}

Output : "12345678901234567890"
A C string is a bunch of characters terminated by a '\0' byte. Your char array is
one byte too short to store that trailing '\0' byte so the printf function prints that
following short as if it were two bytes; luckily enough one of the bytes in that
short contains a '\0' value. Change your assignment to b.len= 0x4141 and see
the disaster.

kind regards,

Jos
Aug 14 '07 #2

P: 2
A C string is a bunch of characters terminated by a '\0' byte. Your char array is
one byte too short to store that trailing '\0' byte so the printf function prints that
following short as if it were two bytes; luckily enough one of the bytes in that
short contains a '\0' value. Change your assignment to b.len= 0x4141 and see
the disaster.

kind regards,

Jos
Hi Jos

Thanks for the quick response...

array starts with "0" in this case i have given char str1[20] and string consists of 20 characters "12345678901234567891" It starts with 0 and ends with 19 and there is a space to store '\0' value in the place of str1[20]. only when i assign the value for b.len = 1 then there is a change in str1 value.....

secondly if the struct consist like this

struct a
{
short len;
char str1[20];
};

in this case first variable is short declared then comes char....the order is changed in the above structure. now iam not getting the issue. its working fine.

why is the difference? is there any order is maintained internally.

Please advice me...

awaiting for ur reply
Aug 14 '07 #3

Expert 10K+
P: 11,448
Hi Jos

Thanks for the quick response...

array starts with "0" in this case i have given char str1[20] and string consists of 20 characters "12345678901234567891" It starts with 0 and ends with 19 and there is a space to store '\0' value in the place of str1[20]. only when i assign the value for b.len = 1 then there is a change in str1 value.....

secondly if the struct consist like this

struct a
{
short len;
char str1[20];
};

in this case first variable is short declared then comes char....the order is changed in the above structure. now iam not getting the issue. its working fine.

why is the difference? is there any order is maintained internally.

Please advice me...

awaiting for ur reply
If you define a char str1[20] you have 20 characters available: str1[0], str[1] ... str[19].
See, there is no str1[20] and you fill up all 20 available chars starting at index 0,
ending at index value 19; that's 20 characters and none left for the '\0' character
that is supposed to terminate your string.

kind regards,

Jos
Aug 14 '07 #4

P: 16
If you define a char str1[20] you have 20 characters available: str1[0], str[1] ... str[19].
See, there is no str1[20] and you fill up all 20 available chars starting at index 0,
ending at index value 19; that's 20 characters and none left for the '\0' character
that is supposed to terminate your string.

kind regards,

Jos
It's very easy to make mistakes like that with arrays, often with catastrophic consequences. To avoid such problems, you might want to consider using an std::string instead, unless there's a good reason not to.
Aug 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.