Thulasi
Thulasi

Reputation: 29

remove duplicates in a char array of pointers

I have array of pointers in liste_tmp with values "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

Upvotes: 0

Views: 207

Answers (2)

user1781290
user1781290

Reputation: 2874

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.

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:

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

Upvotes: 2

user694733
user694733

Reputation: 16043

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

Upvotes: 3

Related Questions