开发者

verify each element to one other in a array pointer

开发者 https://www.devze.com 2023-02-11 03:18 出处:网络
C Experts, I have an array of pointers to strings. I need to comp开发者_开发技巧are each array element with all other array elements and throw error if they are same. Here is the piece of code I have

C Experts, I have an array of pointers to strings. I need to comp开发者_开发技巧are each array element with all other array elements and throw error if they are same. Here is the piece of code I have written and got stuck. Please help me.

# define FOUND 1
# define NOTFOUND 0

int k,flag,a;
char cmp_string[10]; //used to get one array element to compare with all other array elements

char *values[]={010,020,030,040}; //valid case that's how it should be
char *vales[]={010,020,020,030}; wrong or throw error because in array i should have only unique values

int size=4;
for(k=0; k<=size;k++){
    strcpy(values[k],cmp_string);
    flag=NOTFOUND;

    int counter=k+1;

   for(int n=counter;n<=size;n++)
   {
       a=((strcmp(values[n],cmp_string) || (strcmp(values[k-1],cmp_string)))
      // stuck here what if k value is 2 I wont be able to compare with zero or first element of array.

       if(a==0){
        throw error same name for the operation
        flag=FOUND;
        break;
   }
 }//for int n;

}//for int k;

if(flag==NOTFOUND){
    True or PASS
}
}


Quick solution: sort the array (using e.g. the builtin qsort function), then scan it comparing adjacent elements; if two are the same, you have a repetition.

You can also know before completing the sort that you have duplicates if in the comparison function you find that the two compared items are the same.


If I understand your question correctly, you're trying to turn strcmp into something that returns nonzero if the strings are the same and zero otherwise:

a = (strcmp(whatever) != 0) || (strcmp(whatever else) != 0);
0

精彩评论

暂无评论...
验证码 换一张
取 消