# Thread: linear search for structure (record) array

1. ## linear search for structure (record) array

ok guys, im looking to use a linear search to search a structure for a certain account number. the structure is as follows.

Code:
```typedef struct {
int acctNum;
char lastName[ 15 ];
char firstName[ 10 ];
double balance;
} clientData;```
i have called the function containing the linear search however my actual search algorithm for this wrong. a linear search on a normal array is much easier and im finding troubles determining how to complete the search. this is what i have.

calling funtion using a previously found search key.

Code:
`element = linearSearch (customer, searchKey, SIZE)`
then this is my function for the linear search.

[code]

int linearSearch ( clientData client [], int Key, int arrsize){

int n;

for ( n = 0; n <= arrsize - 1; n++ )
if (strcmp (client[n] .acctNum, Key) == 0)
return n;

return -1;
}

can anyone advise me of how to alter this linear search algorithm so that is searches for an account number that is requested?

2. strcmp() is for comparing C-strings.
acctNum and Key are int's, so simply use "==".

gg

3. ok well thats stopped the program crashing however now it just ends
should i have acctNum. acctNum in there? or doesn't that make sense

4. >for ( n = 0; n <= arrsize - 1; n++ )
The customary idiom is
Code:
`for ( n = 0; n < arrsize; n++ )`
>if (strcmp (client[n] .acctNum, Key) == 0)
Perhaps
Code:
`if (client[n] .acctNum == Key)`
>ok well thats stopped the program crashing however now it just ends
Then show us your new code.