hey guys ! so im learning c, got a question:

so say i have a struct of students and inside the struct is name , id , fatherName, motherName.

like this

struct stds {

int id;
char name[21];
char fatherName[21];
char motherName[21];

} std[100];

suppose they are 100 stds and i want to find them by typing id.....

printf ("Search for a student record, type id: ");
scanf ("%i",&searchid);

I want to view their name ,fathers name and mothers name by typping his id, how would i do that?

Thanks brouss !

Edited 2 Years Ago by centenond

Well this is how you access an element of std[100] and get its elements.

std[searchid].id
std[searchid].name
std[searchid].fatherName
std[searchid].motherName
Comments
(y)
Well this is how you access an element of std[100] and get its elements.

std[searchid].id
std[searchid].name
std[searchid].fatherName
std[searchid].motherName

how i didnt think of that! how stupid stupid ....
Thanks! u rock!

oh w8 but if i do that i will be searching for the array number
and not for the custom id number right?

Yes, you will searching for the element contained in the array std. If you need to search id in the array std then you could loop through std and check each id.

size_t i = 0;

for (; i < 100; ++i)
{
    if (std[i].id == some_int)
    {
        //do something here
    }
}

i thought about that, just couldnt quite put it toghether xD
Thanks again.

This question has already been answered. Start a new discussion instead.