0

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 ?

4
  • 1
    You need strcmp(uidDB[i], voterID). Also, you need to pass in the size of the array. sizeof(uidB[0]) gives the size of a char * which is not what is wanted. Commented Aug 14, 2020 at 0:40
  • @kaylum Thanks for the reply. Pls could you be a little more clearer, I'm new to c. I've tried getting the length of the array using sizeof(uidDB[0]) and (sizeof(uidDB) / sizeof(uidDB[0])). I get 4, 3 respectively. But using the second instance in the helper function I get another error: Commented Aug 14, 2020 at 1:19
  • @kaylum ./03.c: In function 'verifyVoterID': ./03.c:16:30: warning: 'sizeof' on array function parameter uidDB' will return size of 'char **' [-Wsizeof-array-argumen ] 16 | for (int i = 0; i < (sizeof(uidDB) / sizeof(uidDB[0])); i++) | ^ ./03.c:13:25: note: declared here 13 | int verifyVoterID(char *uidDB[], char *voterID) | ~~~~~~^~~~~~~ Commented Aug 14, 2020 at 1:19
  • 3
    You can't calculate size of an array that is passed into a function from within the function. Need to get the size in the caller and pass that into the function. How do you get the size of array that is passed into the function? Commented Aug 14, 2020 at 1:24

1 Answer 1

3

There're many things to modify in my point of view and I am posting the following code with some comments. I hope this helps you. It was tested on VS2019.

// return 0 if uidDB[i]==voterID, -1 if no match found
// I added const keyword since we're not going to change uidDB or voiterID in the function(read-only)
int verifyVoterID(const char* uidDB[], int uidLen, const char* voterID)
{
    for (int i = 0; i < uidLen; ++i)
    {
        if (!strcmp(uidDB[i], voterID)) // same as strcmp()==0.
            return 0;
    }
    return (-1);
}

int main(void)
{
    char voterID[8];    // NOT char* voterID;
    printf("\n\nEnter ID: ");
    scanf("%3s", voterID);  // read 3 characters only.

    const char* uidDB[] = { "001", "002", "003" };  // must be array of const char*
    const int uidDBLen = (sizeof(uidDB) / sizeof(uidDB[0])); // practical way to cal. size of array
    int verify_res = verifyVoterID(uidDB, uidDBLen, voterID);   // always pass the size of array together with the array

    if (verify_res == 0)
        doSomething();  // verify success
    else
        doSomethingElse(); // fail

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.