finding duplicates from the array of pointers

前端 未结 3 1369
太阳男子
太阳男子 2020-12-22 03:35

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 f

3条回答
  •  佛祖请我去吃肉
    2020-12-22 04:35

    char *a[20]; // an array of char pointers. 20 elements in the array.
    
    char *findduplicates(char *arr[3],int count)
    // arr is an array of char pointers, with 3 elements in the array.
    
    findduplicates(a, count); // this will just pass 3 of the 20 pointers
    // thus when you try to access a[3], it will probably seg-fault.
    

    try this instead:

    char *findduplicates(char **arr,int count)
    

    [edit] Maybe I'm wrong about this. I just wrote a small test program, and it wouldn't crash even if declared the function parameter as char *a[0]. But try my suggestion anyway. Memory bugs don't always make stuff crash...

提交回复
热议问题