# Thread: How to get index of structure elements?

1. ## How to get index of structure elements?

Hi
I want to get the starting index of structure elements, whoz id are 0,1,2,3
Like in below code
col_data[0] (starting id=0)
col_data[3] (starting id=1)
col_data[5] (starting id=2)
col_data[8] (starting id=3)

Code:

Code:
```typedef struct gg{
int id;
int value;
} gg;

gg col_data[10]={{0,44},{0,46},{0,39},{1,25},{1,10},{2,46},{2,78},{2,55},{3,64},{3,79}};
int ptr[4];

for (i = 0; i < 4; i++){
for (j = 0; j < 10; j++){
if (col_data[j].id==i){
ptr[i]=j;
// If i use break it will break after first match and will not go again to the loop.
break;
}
}
}
```

output should be
ptr[0]=0
ptr[1]=3
ptr[2]=5
ptr[3]=8

How can i skip remaining loop iterations when it get that index and will go back to loop again for getting next element index?

2. Code:
```for (i =0, j = 0; i < 4 && j < 10; ++j)
{
if (col_data[j].id == i)
{
ptr[i++] = j;
}
}```

3. It won't work.. continue looping and the result was wrong too

Originally Posted by DRK
Code:
```for (i =0, j = 0; i < 4 && j < 10; ++j)
{
if (col_data[j].id == i)
{
ptr[i++] = j;
}
}```

4. I think it is because i was trying this with 0,2,1,3
Code:
```// What is the efficient way of doing this in this case?
gg col_data[10]={{0,44},{0,46},{0,39},{2,25},{2,10},{1,46},{1,78},{1,55},{3,64},{3,79}};```

5. Originally Posted by gevni
I think it is because i was trying this with 0,2,1,3
Sorting col_data by id would be cool.
Otherwise, searching for indexes in a meshed up col_data will lead to two nested loops.

//Edit

Are ids distinguished and contiguous ? If so, then loop once over the table and store each unique reference to a helper table.

6. Originally Posted by gevni
It won't work.. continue looping and the result was wrong too
No, it won't continue. i < 4 condition terminates loop when all 4 indexes are found.

Here's a new solution for you:
Code:
```for (i = 0; i < 4; ++i)
{
ptr[i] = -1; // index not yet found
}

for (i = 0, j = 0; i < 4 && j < 10; ++j)
{
int id = col_data[j].id;
if (id < 4 && ptr[id] == -1)
{
ptr[id] = j;
++i;
}
}```

7. Thanks for your help. It work fine if col_data[10] is in below order

Code:
```gg col_data[10]={{0,44},{0,46},{0,39},{1,25},{1,10},{2,46},{2,78},{2,55},{3,64},{3,79}};
```
I am sorry i provide the data into wrong order it should be like that

Code:
```gg col_data[10]={{3,44},{3,46},{2,39},{2,25},{2,10},{1,46},{1,78},{0,55},{0,64},{0,79}};
```
In that case if i run the above code it shows me output as
Code:
```ptr[0]:7
ptr[1]:0
ptr[2]:0
ptr[3]:0```
As it find the first element in the last of array. How can i copup with it?

Originally Posted by DRK
No, it won't continue. i < 4 condition terminates loop when all 4 indexes are found.

Here's a new solution for you:
Code:
```for (i = 0; i < 4; ++i)
{
ptr[i] = -1; // index not yet found
}

for (i = 0, j = 0; i < 4 && j < 10; ++j)
{
int id = col_data[j].id;
if (id < 4 && ptr[id] == -1)
{
ptr[id] = j;
++i;
}
}```

8. Thank you for your help.. It works