Solved

Copying Structures

Posted on 1998-06-01
6
197 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 50 total points
ID: 1251076
memcpy(&temp2, &temp1, sizeof temp1); will work.
temp2 = temp1; will also work.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 147
C: GetDiskFreeSpaceEx() for default drive 14 107
How to organize data in excel ? 2 114
List out all word 7 292
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

773 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