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. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    20

    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!
    Last edited by porstart; 11-24-2010 at 03:47 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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));
    ?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    20
    Quote Originally Posted by itsme86 View Post
    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
    Last edited by porstart; 11-24-2010 at 04:12 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How about...

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

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    20
    Quote Originally Posted by CommonTater View Post
    How about...

    Code:
    struct mystruct temp;
    temp = info[a];
    info[a] = info[b];
    info[b] = temp;
    Thank you. I thought I'd already tried that ;\

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  2. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21