1. ## Data Structures

I want to use data structures to create like a 2D array (x,y) and then sort them by looking at x. The code is below but when I sort it, it sorts only the x's.. the corresponding y value doesn't stick. I know I am CHANGING the value of x's so there's no way y would also rearrange. How do I change this so that the both the x and y are sorted. Can I swap pointer locations like a pointer to the structure which will maintain the coressponding y values with x?

Code:
```#include <stdio.h>
#include <string.h>

int main() {

int i, item, hold;

struct Points {
int x;
int y;
};

struct Points ptA[4];

for (i=0; i<4; i++) {
fflush(stdin);
printf("Enter #%d point: ", i+1);
scanf("(%d,%d)", &ptA[i].x, &ptA[i].y);
}

printf("\n");

for (item=0; item<4-1; ++item)
for (i=item+1; i<4; ++i) {
if (ptA[i].x<ptA[item].x) {
hold = ptA[item].x;
ptA[item].x=ptA[i].x;
ptA[i].x=hold;
}
}

printf("sorted:\n");
for (i=0; i<4; i++)
printf("(%d,%d)\n", ptA[i].x, ptA[i].y);

printf("\n");

return 0;
}```

2. Well, you're only changing the x values of the structures... And not the y values.
Try making hold a struct Points and then swap the structures. Kind of like this...
Code:
```hold = ptA[item];
ptA[item] = ptA[i];
ptA[i] = hold;```
You can use = assignment with structs of same type, but not == comparison.

3. Originally Posted by Rouss
You can use = assignment with structs of same type
Yes, you can. But remember, if your structure contains pointers, all it does is assign one pointer to the other. It won't duplicate anything you have allocated for said pointer. Thus, if you're trying to clone a structure, you won't really make a copy of data pointed to, rather just another "link" to the same object.

Quzah.