?
Solved

Copying Structures

Posted on 1998-06-01
6
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1251074
No. But it will work if you use memcpy.

0
 

Author Comment

by:kulesk
ID: 1251075
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 200 total points
ID: 1251076
memcpy(&temp2, &temp1, sizeof temp1); will work.
temp2 = temp1; will also work.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:alexo
ID: 1251077
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
ID: 1251078
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
ID: 1251079
Thank You!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

764 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