从指针数组发现重复 [英] finding duplicates from the array of pointers

查看:155
本文介绍了从指针数组发现重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到pointers.the code数组重复的present是below.when显示我运行这个应用程序是gining分割fault.But当我解压这个功能,我能够仅运行fine.Can谁能告诉我有什么事情`

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 `

当我检测到重复的,我只是把这些字符串到一个名为output.txt的文件中。

when i detect the duplicates,i just put those strings to a file named output.txt.

我发现,使用STRCMP时,它给这个分段错误。
但是当我提取此功能,并在一些测试code运行它,它的作品真的很好。

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

推荐答案

我觉得这部分将是问题的根源:

I think this part would be the root of the problem:

    while ((dit = readdir(dip)) != NULL)               
       {
           condition_var = dit->name;      
           a[i] = condition_var;  
                         i++;
       }

我不知道READDIR()是如何工作的,但我猜想,每一次它会在时间结构的指针返回DIT。一个[我],保存condition_var作为指针的名字。这可能是以后再次调用READDIR()制成并且使用精确相同的号负责空间返回时间结构,覆盖纪念品空间,一个previous condition_var指向

I am not sure about how readdir() works but I would guess that every time it will return a temporal structure pointer to dit. a[i] saves condition_var as the pointer to the name. It is possible that later another call to readdir() is made and uses the exact the same mem space to return the temporal structure, overwriting the mem space that a previous condition_var pointing to.

我会建议一个更安全的方式做到这一点的一部分,这可能是一个稍微复杂一些,虽然。

I would suggest a safer way to do this part, which may be a little more complex, though.

    while ((dit = readdir(dip)) != NULL)               
       {
           int len = strlen(dit->name);
           condition_var = malloc(len+1);
           memncpy(condition_var, dit->name, len+1);
           //condition_var = dit->name;      
           a[i] = condition_var;  
                         i++;
       }

记住要自由的时候malloced,在节目的最后每块纪念品。 (或者你可以只留下这些到OS收集纪念品 - 不推荐)。在code应该是这样的:

Remember to free every mem block that you malloced, at the end of the program. (Or you can just leave all these to OS to collect the mem--not recommended). The code should be like this:

     int j; // use another loop indicator if you already declared j for other uses.
     for(j = 0; j<i; j++)   // i is the length of a[].
     {
         free(a[j]);   
         // no worry about a[j] being NULL. free() does nothing on a NULL pointer.
         a[j] = NULL;
     }

这篇关于从指针数组发现重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆