0

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

3
  • 1
    Are you familiar with strcmp()? And I assume you clipped out the code where you actually properly initialize liste_tmp[] and no_rlt_liste[] ? Commented Nov 29, 2013 at 8:11
  • What is n in for (i = 0 ; i < n; i++)? Commented Nov 29, 2013 at 8:13
  • You will probably need to use strcmp() to compare strings, probably in place of if (liste_tmp[i] == no_rlt_liste[j]). Commented Nov 29, 2013 at 8:19

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

While presumptive on my part, I've a suspicion the code posted has all of the initialization (the arrays, count, etc) omitted. Still, this seems correct, so +1, though I still think the comparison is wrong.
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.

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;
   }

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.