i wanted to find the duplicates present in an array of pointers.the code is shown below.when i run this application it is gining segmentation fault.But when i extract this function,i am able to run it just fine.Can anyone please tell me what could `
when i detect the duplicates,i just put those strings to a file named output.txt.
i discovered that when strcmp is used,its giving this segmentation fault. but when i extract this function and run it on some test code,it works really 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
arrnull-terminated properly?