473,465 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Struct C

2 New Member
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
4 1618
JosAH
11,448 Recognized Expert MVP
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
sudhakarmv
2 New Member
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
JosAH
11,448 Recognized Expert MVP
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
TripleDES
16 New Member
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

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

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.