473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

doubt in struct assignment

Consdier the following program:

#include <stdio.h>

struct typeRecord {
int id;
char str[100];
} records[] = {
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"}
};
int main(void)
{
struct typeRecord rec1 = {10, "ten"};
int i;

for (i = 0; i < 10; ++i)
{
rec1 = records[i];
printf("%d %s\n", rec1.id, rec1.str);
}

return 0;
}

The above program produces the following output in both gcc and VC++.

0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine

My Doubt:
--------------
character arrays cannot be assigned directly. However the above output
shows that if character array is a member of a structure, the
assignment of structures produces copying of strings as if copied
using strcpy. Is this the expected behaviour of structure assignment ?

Feb 17 '07 #1
5 1996
subramanian wrote:
>
My Doubt:
--------------
(Question)
character arrays cannot be assigned directly. However the above output
shows that if character array is a member of a structure, the
assignment of structures produces copying of strings as if copied
using strcpy. Is this the expected behaviour of structure assignment ?
Structures are copied byte for byte, the types of the members does not
matter. If a structure is 100 bytes long, 100 bytes are copied.

--
Ian Collins.
Feb 17 '07 #2
Ian Collins wrote:
subramanian wrote:

My Doubt:
--------------
(Question)
character arrays cannot be assigned directly. However the above output
shows that if character array is a member of a structure, the
assignment of structures produces copying of strings as if copied
using strcpy. Is this the expected behaviour of structure assignment ?

Structures are copied byte for byte, the types of the members does not
matter. If a structure is 100 bytes long, 100 bytes are copied.
The behaviour of the original code is as expected, as you point out,
but the reason is not necessarily correct. Padding bytes need not be
copied, and are not always copied. The reason array assignment doesn't
work is because in most contexts, array names are converted to
pointers to the array's first element, so

int a1[4], a2[4];
int main(void)
{
a1 = a2;
}

is equivalent to

int a1[4], a2[4];
int main(void)
{
&a1[0] = &a2[0];
}

which makes no sense. There is no such problem for structures, even if
they contain array members. It could have been allowed without too
many technical problems, but wasn't, probably because for simplicity,
compilers already didn't support array assignment before C was
standardised, and afterwards, there was not enough value in adding it.

Feb 17 '07 #3
On Feb 17, 12:22Â*pm, "Harald van Dijk" <true...@gmail.comwrote:
The behaviour of the original code is as expected, as you point out,
but the reason is not necessarily correct. Padding bytes need not be
copied, and are not always copied.
Kindly clarify the following:

What is meant by padding of bytes ?

If padding bytes are not copied, will it affect the structure members
from being copied correctly?

Feb 17 '07 #4

subramanian wrote:
On Feb 17, 12:22 pm, "Harald van Dijk" <true...@gmail.comwrote:
The behaviour of the original code is as expected, as you point out,
but the reason is not necessarily correct. Padding bytes need not be
copied, and are not always copied.

Kindly clarify the following:

What is meant by padding of bytes ?
It's not "padding of bytes". It's padding bytes. Padding bytes are
dummy bytes which may be inserted between the members of a struct
object in memory. It's mainly done to honour any hardware alignment
requirements and to speed up the access of the structure object. It's
transparent to the programmer.
If padding bytes are not copied, will it affect the structure members
from being copied correctly?
No, that's something the compiler will take care of. It'll vary from
compiler to compiler and even compilation to compilation with the same
compiler. Once again, any changes, if any, will be transparent to the
programmer and it "Just Works".

PS. Even scalar objects like int or long are allowed to have padding
bits, but in practise this is much rarer than padding bytes for
structures.

Feb 17 '07 #5
"santosh" <sa*********@gmail.comwrites:
subramanian wrote:
>On Feb 17, 12:22 pm, "Harald van Dijk" <true...@gmail.comwrote:
The behaviour of the original code is as expected, as you point out,
but the reason is not necessarily correct. Padding bytes need not be
copied, and are not always copied.

Kindly clarify the following:

What is meant by padding of bytes ?

It's not "padding of bytes". It's padding bytes. Padding bytes are
dummy bytes which may be inserted between the members of a struct
object in memory. It's mainly done to honour any hardware alignment
requirements and to speed up the access of the structure object. It's
transparent to the programmer.
>If padding bytes are not copied, will it affect the structure members
from being copied correctly?

No, that's something the compiler will take care of. It'll vary from
compiler to compiler and even compilation to compilation with the same
compiler. Once again, any changes, if any, will be transparent to the
programmer and it "Just Works".
Yes, theoretically padding bytes can vary from compilation to
compilation with the same compiler, but in practice it's likely to be
consistent. Storing data by writing structures directly to files is
often a bad idea, but you can usually get away with it as long as you
use the same implementation. If this weren't the case, you might
recompile your program and lose the ability to read the data files you
just created.

But some compilers do support compile-time options that can change
data layout.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
4
by: James Harris | last post by:
Having updated my Debian system it now complains that I am using an incompatible pointer type in warnings such as "passing arg 2 of 'bind' from incompatible pointer type" from code, struct...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
28
by: WaterWalk | last post by:
Hi, I'm haunted by 2 questions about struct copy. Though I searched the net, but still in confusion. 1. Does struct assignment copies every member including array members? For example, struct...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
6
by: krishna | last post by:
Hi , When i read linux tcp/ip stack codes i come cross lot of structure assignment like this. I guess '=' is the assignment operator and is there any particular reason for using ':' as the...
5
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.