Here's my attempt to loop through an array of strings.
I created an array of ids, compared each id to a voterID which is being read from the console.
// helper function
int verifyVoterID(char *uidDB[], char *voterID)
{
for (int i = 0; i < sizeof(uidDB[0]); i++)
{
if (uidDB[0][i] == voterID)
return 0;
}
return 1;
}
int main()
{
char *uidDB[] = {"001", "002", "003"}, *voterID;
printf("\n\nEnter ID: ");
scanf("%s", voterID);
// Verify voter's ID
if (voterID)
verifyVoterID(uidDB, voterID) == 0
? doSomthing()
: doSomethingElse();
return 0;
}
OUTPUT:
./03.c: In function 'verifyVoterID':
./03.c:19:21: warning: comparison between pointer and integer
19 | if (uidDB[0][i] == voterID)
|
I've also tried using strcmp()
to compare
int verifyVoterID(char *uidDB[], char *voterID)
{
for (int i = 0; i < sizeof(uidDB[0]); i++)
{
if (strcmp(uidDB[0][i], voterID) == 0)
return 0;
}
return 1;
}
OUTPUT:
./03.c: In function 'verifyVoterID':
./03.c:19:24: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]
19 | if (strcmp(uidDB[0][i], voterID) == 0)
| ~~~~~~~~^~~
| |
| char
What am I missing ?