?
Solved

Copying Structures

Posted on 1998-06-01
6
Medium Priority
?
210 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

840 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