473,769 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structure assignment vs memcpy

Suppose I need to assign one structure to another.
Should each member of the structure be assigned individually or can
memcpy be used ?

Since the compiler can add padding bytes, which approach is always
safer ?

Mar 10 '07 #1
2 13139
su************* *@yahoo.com, India wrote:
Suppose I need to assign one structure to another.
Should each member of the structure be assigned individually or can
memcpy be used ?

Since the compiler can add padding bytes, which approach is always
safer ?
Simply assign one object to another with the assignment operator.
Structure assignments have been supported since C90.

Of course if you only want to assign some members of a struct object
to another, then you'll have to do it manually.

Using memcpy is not usually necessary, since the language directly
supports copying structures.

Mar 10 '07 #2
In article <11************ **********@8g20 00cwh.googlegro ups.com>,
su************* *@yahoo.com, India <su************ **@yahoo.comwro te:
>Suppose I need to assign one structure to another.
Should each member of the structure be assigned individually or can
memcpy be used ?
There seems to be a whole bunch of you doing the same course, asking
the same questions over and over again. Why don't you get together and
nominate one of you to do all the homework?

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 10 '07 #3

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

Similar topics

7
2712
by: Brian K. Michalk | last post by:
I know I can assign default values to a structure during initialization, but I have a routine that accumulates a lot of floats into an array, and then I would like to assign them to a (reference to a) structure before exiting. I would like something analogous to strcpy, but for structures. My example: in file foo.h: ----------------------------------- struct _stats { float slope;
15
38282
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a memcpy(bptr, aptr, sizeof(myStruct));
12
9837
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is there a nicer way of doing this in a single statement?
4
1563
by: gladhuman | last post by:
Hi, Let's assume that I have the following structure and the following array: struc My_struct { char var_one; char var_two; char var_three;
2
4126
by: Rick Anderson | last post by:
I want to store a "va_list" in a structure (which gets passed around from function to function). I cannot use the va_copy() routine to create a copy of the varargs (it looks like it uses stack memory). Has anyone written some portable code to create a copy of the varargs information, and subsequent routines to retrieve the varargs information? Thanks in advance! Rick
38
2261
by: sam_cit | last post by:
Hi, I have the following Struct, Struct Sample { int i; char *p; };
7
1520
by: Pep | last post by:
I'm getting weird results at the moment so thought I'd rebase my knowledge of c++ storage with your help :) I have a class used as a type in a struct and the struct is handled by a 3rd party binary writed for persistent storage. I am confused by the results I am getting and am not sure how the 3rd party writer is seeing the size of the stuct. class foo // I have not included the methods for brevity
5
2280
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the overloaded operator function performs the necessary copy of obj's member in to obj1. Can't the same be done using copy
6
1437
by: James H. Newman | last post by:
I have the following: typedef unsigned char T ; T y = { 0x22, 0x33 } ; I now define unsigned short x ;
0
9590
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...
1
10000
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
9866
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...
0
8879
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.