How to find out the position of an array

Hi all, Im doing a program to store student data. I initialise a Structure and a student array of 50. like

Stdinfo Student[50];

Let say if I store a name Johnson in Student[45], how can I find out the position of the array which store the name Johnson?

any advice will be appreciated.
Last edited on
you have to iterate through the array until you find it. Unless your array is sorted by name, the you can use a binary search etc. There is no magic way.
Just to add a simple example of what Zaita is talking about

1
2
3
4
5
6
7
8
9
10
11
12

int pos = -1;
for (int i = 0; i < 50 && pos == -1; i++)
{
    // Assuming there is a char[] field called name in Stdinfo
    if (strcmp(Student[i].name, "Johnson") == 0)
    {
        pos = i;
    }
}
// variable pos is now set to the array index of Johnson if Johnson exists
// otherwise it will stay at -1 
Last edited on
Thanks lots guys
Topic archived. No new replies allowed.