# Swapping the indices of a structure

This is a discussion on Swapping the indices of a structure within the C Programming forums, part of the General Programming Boards category; I'd like to do this for my selection sort. I can do it for a member of a structure, e.g. ...

1. ## Swapping the indices of a structure

I'd like to do this for my selection sort.

I can do it for a member of a structure, e.g. if STUCT info[] is a structure and name is a string member of the structure:

Code:
```strcpy(temp, info[a].name);
strcpy(info[a].name, info[b].name);
strcpy(info[b].name, temp);```
I'm trying to create a swap which causes every member of the structure for a particular index to swap it's index (not just for a specific member like in the example I provided).

I hope that's clear.

Thanks for any help!

2. You mean something like:

Code:
```struct mystruct temp;
memcpy(&temp, &info[a], sizeof(temp));
memcpy(&info[a], &info[b], sizeof(temp));
memcpy(&info[b], &temp, sizeof(temp));```
?

3. Originally Posted by itsme86
You mean something like:

Code:
```struct mystruct temp;
memcpy(&temp, &info[a], sizeof(temp));
memcpy(&info[a], &info[b], sizeof(temp));
memcpy(&info[b], &temp, sizeof(temp));```
?
I'm 90% sure memcpy isn't covered in my course.

Basically if I have a structure which I've filled, how would I swap the 1st index with the 2nd index for every member of the structure?

So that if I'm printing the structure's members, if it starts out like this:
one two three //all index one
four five six // all index two

It will become this after sorting:
four five six //new index one
one two three //new index two

Code:
```struct mystruct temp;
temp = info[a];
info[a] = info[b];
info[b] = temp;```

5. Originally Posted by CommonTater
```struct mystruct temp;