Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从非有序数组中消除重复的元素_C_Pointers_Dynamic Memory Allocation - Fatal编程技术网

C 从非有序数组中消除重复的元素

C 从非有序数组中消除重复的元素,c,pointers,dynamic-memory-allocation,C,Pointers,Dynamic Memory Allocation,编写函数: int-different(int-input[],int-size,int**vetout); 当给定一个维数为的整数数组输入时,会在动态内存中创建一个向量,其中包含重复一次的所有输入元素。函数different返回修改后数组的大小。 我的问题是,我在编译程序时遇到了分段错误。谁能帮帮我吗 #include<stdio.h> #include<stdlib.h> int different(int input[],int size, int **vet

编写函数: int-different(int-input[],int-size,int**vetout); 当给定一个维数为的整数数组输入时,会在动态内存中创建一个向量,其中包含重复一次的所有输入元素。函数different返回修改后数组的大小。 我的问题是,我在编译程序时遇到了分段错误。谁能帮帮我吗

 #include<stdio.h>
 #include<stdlib.h>

int different(int input[],int size, int **vetout);

int main(){
    int input[]={1,1,2,3,4,4,1,2,3,4};
    int size=10; int n; int i; int *vetout;
    n = different(input, size, &vetout);
    printf("The size is : %d", n);
    for(i=0;i<n;i++){printf("The array is : %d",vetout[i]);}
    return 0;
}

int different(int input[],int size, int **vetout){
    int i=0;int j,k;
    while(i<size){
        j=i+1;
        while(j<size){
            if(input[i]==input[j]){
                for(k=j;k<=size;k++){
                    input[k]=input[k+1]; size--;
                }
            }
            j++;
        }
        i++;
    }

    *vetout=(int *)malloc(sizeof(int)*size);
    printf("The size is : %d",size);
    for(i=0;i<size;i++){*vetout[i]=input[i];}

    return size;
}
#包括
#包括
int-different(int-input[],int-size,int**vetout);
int main(){
int输入[]={1,1,2,3,4,4,1,2,3,4};
int size=10;int n;int i;int*vetout;
n=不同(输入、大小和输出);
printf(“大小为:%d”,n);

对于(i=0;i我修改了您的函数,请随意使用它作为基础-这不是解决您问题的最佳解决方案-preety的评论在原始实现中涵盖了您的问题

希望这能作为指导方针

int different(int input[],int size, int **vetout){

    int count = 0;
    int found, i, j;
    *vetout = malloc(sizeof(int)*size);

    for ( i=0; i<size ; i++ ) {
        // this loop will iterate on each element of the array
        // and check if it was already listed
        found = 0;
        for ( j=0; j < count ; j++ ) {
            //this loop checks if the value was already set in the output vector
            if ( *(*vetout+j) == input[i] ) {
                found = 1;
            }
        }

        //if it was not set - then set it and increse the index 'count'
        if ( !found )
        {
            *(*vetout+ (count++)) = input[i];
        }
    }

    printf("The size is : %d\n", count);

    return count;
}
int-different(int-input[],int-size,int**vetout){
整数计数=0;
int,i,j;
*vetout=malloc(sizeof(int)*大小);

for(i=0;i)循环太复杂。您需要将输入[i]与所有输入[0…i-1]进行比较,如果值是新的,则添加到vetout。当然,vetout应该在循环之前分配。不要更改原始输入数组。在
中(k=j;kYou应该为输出使用不同于
size
的变量,因为
size--
缩短了输入。欢迎使用堆栈溢出!请回答您的问题,告诉我们您进行了什么样的调试。我希望您已经运行了InSide Valgrind或类似的检查器,并使用诸如G之类的调试器进行了调查例如DB。请确保您也启用了一整套编译器警告。这些工具告诉了您什么,它们缺少什么信息?请阅读Eric Lippert的。请阅读并理解。