'strcmp' C 语言中的字符数组


...
main() {
   char **src_ip[10];
   char **dest_ip[10];
   char **lat[10];
   char *ip[20];
   while{
      //Read file
      //fgets();
      src_ip[j] = &data[0];
      dest_ip[j] = &data[1];
      lat[j] = &data[2];
      int idx;
      int addip;
      for(idx=0; idx<20; idx++)
      {
          addip = 0;
          //Check to see if the IP address is already in the array.
          if ((strcmp(*(src_ip[j]),  ip[idx]) == 0) ||
              (strcmp(*(dest_ip[j]), ip[idx]) == 0))
          {
              addip=1;
              break;
          }
          //If the IP address was already found then addip would equal 1.
          if (!addip){
              printf("new node: %s",*(ip[idx]));
          }
          else
              printf("Exist");
      }
   }
   j++;
}

我想比较两个 1d 数组中的字符字符串 - 数组 src_ip[j]dest_ip[j] - 并插入到一个数组 ip[idx] 中。例如,当我查找ip[1]时,它将转到src_ip[1],这意味着它仅引用一个数组 - ip[idx] - 而不是分别查看src_ipdest_ip

代码中一定有问题 - 也许我遗漏了一些东西?

if((strcmp(*(src_ip[j],ip[idx]) == 0) && (strcmp(*(dest_ip[j]),ip[idx]) == 0))
                     ^^

您不能同时拥有*(src_ip[j])*(dest_ip[j])等于ip[idx]。您打算使用的是||运算符而不是&&运算符。(顺便说一下,我在上面标记的地方缺少一个)(

你的while(idx)也没有意义。您是使用上面的for还是下面的while来控制循环?如果使用 while ,那么如果条件为 true,您将获得一个无限循环,因为您永远不会在循环中更改它。

最新更新