删除char数组指针中的重复项

时间:2021-05-22 21:15:46

I have array of pointers in liste_tmp with values "123" , "456" , "123"

我在liste_tmp中有一些指针数组,值为“123”,“456”,“123”

Declaration :

char  *no_rlt_liste[5] , *liste_tmp[5]; int i, j, count =0;
for (i = 0 ; i < n; i++){
    for ( j= 0 ; j< count; j++){
            if (liste_tmp[i] == no_rlt_liste[j]){
                    break;
            }
            if (j == count){
                    no_rlt_liste[count]  = liste_tmp[i];
                    printf(" ENTER\n");
                    count++;
            }
    }
}
for (i = 0 ; i < count; i++)
    printf("Final result %s\n", no_rlt_liste[i]);

the above code doesn't produce result. not able to identify the bug. any help? Thanks

上面的代码不会产生结果。无法识别错误。任何帮助?谢谢

2 个解决方案

#1


3  

Your for loops never run because of the condition j< count, and you have set count =0.

由于条件j ,你的for循环永远不会运行,你设置count>

#2


2  

You initialize count to 0 which cause the inner for loop to not execute (since j < 0 is always false), thus your whole loop doing nothing.

您将count初始化为0会导致内部for循环不执行(因为j <0始终为false),因此整个循环不执行任何操作。

for (i = 0 ; i < n; i++)
{
  int flag = 0;
  for (j= 0; j< count; j++)
  {
    if (liste_tmp[i] == no_rlt_liste[j])
    {
      flag = 1
      break;
    }
  }
  if (!flag)
  {
      no_rlt_liste[count]  = liste_tmp[i];
      printf(" ENTER\n");
      count++;
  }
}

Also, be aware that you need to use strcmp if you do not want to compare the char-pointers, but their contents instead:

另外,请注意,如果您不想比较char-pointers,则需要使用strcmp,而不是它们的内容:

   if (strcmp(liste_tmp[i], no_rlt_liste[j]) == 0)
   {
      flag = 1
      break;
   }

#1


3  

Your for loops never run because of the condition j< count, and you have set count =0.

由于条件j ,你的for循环永远不会运行,你设置count>

#2


2  

You initialize count to 0 which cause the inner for loop to not execute (since j < 0 is always false), thus your whole loop doing nothing.

您将count初始化为0会导致内部for循环不执行(因为j <0始终为false),因此整个循环不执行任何操作。

for (i = 0 ; i < n; i++)
{
  int flag = 0;
  for (j= 0; j< count; j++)
  {
    if (liste_tmp[i] == no_rlt_liste[j])
    {
      flag = 1
      break;
    }
  }
  if (!flag)
  {
      no_rlt_liste[count]  = liste_tmp[i];
      printf(" ENTER\n");
      count++;
  }
}

Also, be aware that you need to use strcmp if you do not want to compare the char-pointers, but their contents instead:

另外,请注意,如果您不想比较char-pointers,则需要使用strcmp,而不是它们的内容:

   if (strcmp(liste_tmp[i], no_rlt_liste[j]) == 0)
   {
      flag = 1
      break;
   }