I wanted to find duplicates present in the array of pointers. The code is shown below. When I run this application, it faces a segmentation problem. But when I extract this function, I can run it just fine. someone will tell me what can`
when I detect duplicates, I just put these lines in a file called output.txt.
I found that when strcmp is used, it gives this segmentation error. but when I extract this function and run it on some kind of test code, it works fine.
main() { char *a[20]; DIR *dip; int i = 0; dip = opendir("src/my_folder"); char *condition_var; while ((dit = readdir(dip)) != NULL) { condition_var = dit->name; a[i] = condition_var i++; } findduplicates(a,i); } char *findduplicates(char *arr[3],int count) { int i = 0; int j = 0; int val = 0; FILE *output = fopen("output.txt","w"); for(i = 0;i<count;i++) { j = i+1; for(;j<count;j++) { if(strcmp(arr[i],arr[j])==0) { printf("The index of a duplicate elemnt is %d\n",j); arr[j] = " "; } } } int k = 0; while(k<3) { printf("the aarray is %s\n",arr[k]); fputs(arr[k],output); fputs("\n",output); k++; }
} `
advanced thanks Maddy
maddy
source share