Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copying Structures

Posted on 1998-06-01
6
Medium Priority
?
209 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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!

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

636 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