char *ar[5];    //Array of pointers

let say char * ar[5] contain: 
ar[0] = "cat"; 
ar[1] = "desk";
ar[2] = "desk"
ar[3] = "zoo";
ar[4] = "cat";

how i want to get of any same strings. so in end ar should have the value "cat","desk","zoo".

this is what i have so far but iam not sure if iam doing this right.
for(i = 0; i < 5; i++)
{
    for(j = i+1; j < 5; i++)
    {
        if(ar[i] == ar[j])
        {
           ar[i] = '\0';
        }
     }
}

//print
for(i = 0; i < 5; i++)
{
    printf("%s",ar[i]);
}

You can't use if(ar[i] == ar[j]) because that is testing if the pointer values are equal not if the string are equal. You need to use if ( strcmp(ar[i],ar[j]) == 0 ) to check if two strings are equal. If strcmp return 0 they are equal. Also there is an error on line 15 above. It should read for(j = i+1; j < 5; j++) were j is incremented not i. Lastly, if you try to do ar[i] = '\0'; in you example you will get an error becasue the values of the ar strings are in a read-only part of memory because they are hard coded. Test it like:

#include <stdio.h>
#include <string.h>
int main(){
   int  i,j;
   //Array of pointers
   char ar[5][5];  // Don't forget to have enough for the '\0'
                   // If strings below are greater then 4, you
                   // will get errors
   sprintf(ar[0],"cat"); 
   sprintf(ar[1],"desk"); // <-- Since 4 chars array must be at least 5
   sprintf(ar[2],"desk");
   sprintf(ar[3],"zoo");
   sprintf(ar[4],"cat");

   for(i = 0; i < 5; i++) {
       for(j = i+1; j < 5; j++) {
           if ( strcmp(ar[i],ar[j]) == 0 ){
              ar[i][0] = '\0';
           }
        }
   }
   //print
   for(i = 0; i < 5; i++) {
     if ( strlen(ar[i]))
       printf("%s\n",ar[i]);
   }
}

Output

$ ./a.out
desk
zoo
cat
$

thanks, but i was wondering how to do it with array of pointers?

Its all the same. If I replace the declaration of the array with:

//Array of pointers
char *ar[5]; 
// Allocate memory
for (i=0;i<5;i++)
   ar[i] = (char*)malloc(5);

// No change to the code that follows

// Need to add deallocation
for (i=0;i<5;i++)
   free(ar[i]);

None of the code below would need to change, except for a loop to free the memory allocated.

thanks, i am noob in c and i just want to be 100 % sure i dont mess this up.

#include <string.h>
int main(){
   int  i,j;
   //Array of pointers
   char *ar[5];
               ...

   for(i = 0; i<5; i++)
       ar[i] = (char*)malloc(5);

   for(i = 0; i < 5; i++) {
       for(j = i+1; j < 5; j++) {
           if ( strcmp(ar[i],ar[j]) == 0 ){
              ar[i][0] = '\0';
           }
        }
   }
   //print
   for(i = 0; i < 5; i++) {
     if ( strlen(ar[i]))
       printf("%s\n",ar[i]);
       free(ar[i]);
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.