how to copy a structure to anonger structure

I made 2 structures:
1. struct {} customer[100]
2. struct {} dummycustomer[100]

I need to put all the info in struct {} customer[100] to
struct {} dummycustomer[100].

My purpose is to use the dummy struct for sorting, since sorting would disarange the entries in the array. then I will again refer to the original struct.

here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct
	{
	long int cid;
	char fn [20];
	char mi;
	char ln [20];
	char addr[40];
	long int prev;
	long int curr;
	long int cons;
	float bill;
   }customer [100];


struct
   {
	long int dumcid;
	char dumfn [20];
	char dummi;
	char dumln [20];
	char dumaddr[40];
	long int dumprev;
	long int dumcurr;
	long int dumcons;
	float dumbill;
   }dumcustomer [100]; 

  strcpy (customer[].fn[] , dumcustomer[].dumfn[]); //doesnt work
  mi = dummi; //doesnt work
  strcpy ln [20] , dumln[20]; //doesnt work
  strcpy addr [40] , dumaddr[40]; //doesnt work
  prev = dumprev;  //doesnt work
  curr = dumcurr;  //doesnt work
  cons = dumcons; //doesnt work
  bill = dumbill; //doesnt work 
closed account (z05DSL3A)
You would need to somthing like this:
customer[1].cid = dumcustomer[1].dumcid;


Try using a structure defined as bellow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct customer_t {
	long int cid;
	char fn [20];
	char mi;
	char ln [20];
	char addr[40];
	long int prev;
	long int curr;
	long int cons;
	float bill;
   };
....

customer_t customers[100];
customer_t dumcustomers[100];

... 

customerArray[1] =   dumcustomerArray[1];


Last edited on
Though a memcpy would work with the structures, I would not recommend you to do that as it would a bitwise copy (you would know what it is if you are familiar with that in classes and copy constructures).

Instead of create a dum copy of the entire array of structure I would use one dummy structure (better, a temp variable for each unique type in the structure) and swa the contents as needed.

For example, to swap the contents in a simple sort by your cid element, would look like:

long int tempLongInt;
char tempChar;
char tempString[40]; //take max size of string in your struct
float tempFloat;

for (i = 0, i < maxSize; i++) //where maxSize is of your array size
for (j = i+1; j < maxSize; j++)
if (customers[i].cid > customers[j] )
{
tempLongInt = customers[i].cid;
customers[i].cid = customers[j].cid;
customers[j].cid = tempLongInt;
// do it similarily for other long int variables such as prev, curr, cons

strcpy(tempStr, customers[i].fn);
strcpy(customers[i].fn, customers[j].fn);
strcpy(customers[j].fn, tempStr);
// do it similarily for other ln and addr too

tempChar = customers[i].mi;
customers[i].mi = customers[j].mi;
customers[j].mi = tempChar;

tempFloat = customers[i].bill;
customers[i].bill = customers[j].bill;
customers[j].bill = tempFloat;
}


Thats all. You dont have to create another 100 elements of dummy array where only a simple of temp variables or one dummy array of similar structure is enough.

That simple.

Check it out. Good luck :)
Sorry for the typos: Here it is, revised.

Though a memcpy would work with the structures, I would not recommend that as it would be a bitwise copy (you would know what it is if you are familiar with classes and copy constructures in c++).

Instead of creating a dummy copy of the entire array of structure I would use one dummy structure of same type (better, a temp variable for each unique type in the structure) and swap the contents as needed.

For example, to swap the contents in a simple sort by your cid element, would look like:

long int tempLongInt;
char tempChar;
char tempString[40]; //take max size of string in your struct
float tempFloat;

for (i = 0, i < maxSize; i++) //where maxSize is of your array size
for (j = i+1; j < maxSize; j++)
if (customers[i].cid > customers[j].cid )
{
tempLongInt = customers[i].cid;
customers[i].cid = customers[j].cid;
customers[j].cid = tempLongInt;
// do it similarily for other long int variables such as prev, curr, cons

strcpy(tempStr, customers[i].fn);
strcpy(customers[i].fn, customers[j].fn);
strcpy(customers[j].fn, tempStr);
// do it similarily for other ln and addr too

tempChar = customers[i].mi;
customers[i].mi = customers[j].mi;
customers[j].mi = tempChar;

tempFloat = customers[i].bill;
customers[i].bill = customers[j].bill;
customers[j].bill = tempFloat;
}


Thats all. You dont have to create another 100 elements of dummy array where only a simple set of temp variables or one dummy array of similar structure is enough. That simple.

Check it out. Good luck :)
closed account (z05DSL3A)
satm2008,

the OP stated that they want a copy of the array to sort but still keep the original array unsorted.

And why not edit the typos in your original post rather than post another with the typos corrected.
thtnk you
grey wolf and satm2008,

you are a great help, both of you enlightened me.

The codes you sent me were good but I had to make some adjustments.

I now have a problem with file processing, how to save a structure.
this is the code for my project. Anyone out there who can help will be much appreciated.


cheers karl
Topic archived. No new replies allowed.