473,795 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2025
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*********@gm ail.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_Keit h) 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
2002
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 main features of modules, namespaces, and User-Defined types. The text above was copied from page 31. --------------------------------------------------------
138
5295
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 array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
4
12061
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 sockaddr_in sockad1; .... retval = bind (sock1, &sockad1, sockad1len); I can coerce the pointer with
20
2325
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
3060
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 aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
28
25515
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 A { int n; int m;
37
4027
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 that I implement it as a struct rather than a class which I did and all worked OK. The simplest declaration for the struct is:
6
1625
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 assignment operator.A small snippet as shown below. #include <stdio.h> int main()
5
4054
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 before fwrite(), to shut up memory debuggers like Valgrind about writing uninitialized data to the file. Simplified code: static const struct S default_value = ...; struct S s, t;
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
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 we have to send another system

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.