Solved

Copying Structures

Posted on 1998-06-01
6
193 Views
Last Modified: 2010-04-15
given the following declare:
struct mystruct{
  int  x;
  char this[4];
  char that[4];
}temp1, temp2;

If I want to copy the values of temp1 to temp2, can I do this without having to copy each member individually?  
(ie memset( temp2, temp1, sizeof( temp1 ) ); )?
-kulesk  

0
Comment
Question by:kulesk
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:imladris
Comment Utility
No. But it will work if you use memcpy.

0
 

Author Comment

by:kulesk
Comment Utility
According to your answer, the following should work:
memcpy( temp2, temp1, sizeof( temp1 ) );  
But it doesn't.  I relize this is because I'm not passing in the address of the struct variables.  So you would think the following would work, but it's not.
memcpy( &temp2, &temp1, sizeof( &temp1 ) );
Is it a compiler issue?
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
Comment Utility
memcpy(&temp2, &temp1, sizeof temp1); will work.
temp2 = temp1; will also work.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:alexo
Comment Utility
The first option copies a number of bytes equal to the size of temp1 from the beginning address of temp1 to the beginning address of temp2.

The second option utilizes the "bitwise copy" semantics of the "=" operator when applied to structures.
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
It should perhaps also be noted that the reason

memcpy(&temp2,&temp1,sizeof(&temp1))

doesn't work is that the sizeof will return the size of a pointer to temp1 (2 or 4 bytes depending on your machine) instead of the size of temp1.

0
 

Author Comment

by:kulesk
Comment Utility
Thank You!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now