Thread: how to rearrange structure element on the bases of positions given in other array?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    how to rearrange structure element on the bases of positions given in other array?

    I want to rearrange the positions of structure elements on the bases of perm_array.

    Code:
     typedef struct gg{
      element d;
      int group;
    } gg;
    gg col_data[16];
    int perm_array[4]={2,3,1,0};
    int ptr = 9;
    for (l=0; l<4; l++)
    {
    // i need some statement like that but this statement will re write the value of previous structure element
    col_data[ptr+l]= col_data[ptr+perm_array[l]];
    }

    How can i rearrange structure element like {ptr+2,ptr+3,ptr+1,ptr+0} ?
    Last edited by gevni; 04-08-2013 at 05:23 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Stick to array notation.

    Make two arrays of "gg" structures -> One in the original order (1,2,3,...) and the other the order given by perm_array.

    Use a "for" loop to go through each perm_array element: Something like this
    Code:
    col_data[i] = original_data[perm_array[i]];
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    Thanks.. it work now
    Quote Originally Posted by Click_here View Post
    Stick to array notation.

    Make two arrays of "gg" structures -> One in the original order (1,2,3,...) and the other the order given by perm_array.

    Use a "for" loop to go through each perm_array element: Something like this
    Code:
    col_data[i] = original_data[perm_array[i]];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure.Element or Structure->Element?
    By circuits2 in forum C Programming
    Replies: 8
    Last Post: 10-23-2012, 01:15 AM
  2. dereferencing an element of an array within a structure
    By Flint_Paper in forum C Programming
    Replies: 8
    Last Post: 06-27-2011, 04:52 PM
  3. referencing a structure element from another structure
    By ajitht1986 in forum C Programming
    Replies: 5
    Last Post: 02-08-2011, 03:36 AM
  4. HELP > How to remove element in a Structure Array
    By Inneart in forum C Programming
    Replies: 5
    Last Post: 10-26-2010, 02:40 PM
  5. counting array positions
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 02-04-2008, 11:25 PM