简体   繁体   English

如何遍历字符串数组,将每个字符串与 c 中的 char 字符串进行比较

[英]How to loop through an array of strings, compare each string to a char string in c

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.我创建了一个 id 数组,将每个 id 与从控制台读取的 voterID 进行比较。

// 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: 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我也尝试过使用strcmp()进行比较

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: 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?我错过了什么?

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.它在 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中获取字符串并将其存储在2D char数组中,然后将该2D char数组与C中的字符串进行比较? - How to get the strings from a file and store in a 2D char array and compare that 2D char array with a string in C? 如何将char数组与字符串进行比较 - How to compare an array of char with a string 存储字符串数组以及通过每个字符串进行解析的问题C ++ - Problems with storing array of strings, and parsing through each string C++ 在C中的字符串char数组中搜索字符串 - Searching for string in char array of strings in C c# 尝试遍历字符串数组,比较要查找的字符串,output 找到的字符串 - c# Trying to loop through a string array , compare a string to find, and output the found string 关于char *和数组字符串之间的字符串比较差异的说明 - Explanation about the string compare differences between char* and array strings 如何将字符串字符与结构数组进行比较? - How to compare string char to struct array? 如何遍历字符串数组并将每个元素添加到对象数组 - How to loop through string array and add each element to object array 如何遍历带有字符串数组的字符串以查找匹配项? - How do I loop through a string with an array of strings to find matches? 如何通过char数组搜索String数组? - How to search String array through char array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM