分段错误(核心已转储)gcc ubuntu

时间:2020-05-21 12:40:00

标签: c arrays pointers gcc

我试图通过指针使用一个简单的函数来创建一组用户输入的数字,但是我不断收到此错误,很难判断问题出在哪里,是否还有其他选择可以使用某些东西否则,它会告诉我问题出在哪里,而不是这个奇怪的错误。

#include <stdio.h>

void BuildGroub(int** group,int* count){
    int i=0;
    int j;
    printf("Enter the size of the group \n");
    scanf("%d", &*count);
    while(*count != 0){
        printf("Enter the %d number of the group:\n", i);
        j=0;
        scanf("%d", &**(group+i));
        while(**(group+i)!=**(group+j)){
            j++;
        }
        if(j==i){
            i++;
            count--;
        } else{
            printf("you have already entered this number please try again: \n");
        }
    }
}

int main(){
    int count;
    int group[100];
    int *groupptr = &group;
    BuildGroub(&groupptr,&count);
    for(int i=0;i<count;i++){
        printf("%d, ", group[i]);
    }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

有了这个问题,您不需要使用双指针。如果您想学习如何使用双指针,可以在Google上搜索,然后找到大量示例,例如Double Pointer (Pointer to Pointer) in C

BuildGroub中,您减小了count指针

        if(j==i){
            i++;
            count--;
        }

,但是在while循环的情况下,您比较count指针指向的值。似乎很奇怪。

while(*count != 0)

即使将count--更改为(*count)--,当您退出0循环时,也会减少输入到while的元素数量,然后进入主要功能:

for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.

对于while循环功能,您应该使用temp值,例如:

int size = *count;
while(size != 0){
    ...
    if (i == j) {
       i++;
       size--;
    }
}

例如,您应该使用group[i]而不是*(group+i)。读取代码会更容易。

代码完成:

#include <stdio.h>

void BuildGroub(int* group,int* count){
    int i=0;
    int j;
    printf("Enter the size of the group \n");
    scanf("%d", count);
    int size = *count;
    while(size != 0){
        printf("Enter the %d_th number of the group:\n", i);
        j=0;
        scanf("%d", &group[i]);
        while(group[i] != group[j]) {
            j++;
        }
        if(j==i){
            i++;
            size--;
        } else{
            printf("you have already entered this number please try again: \n");
        }
    }
}

int main(){
    int count;
    int group[100];

    int *groupptr = group;
    BuildGroub(groupptr,&count);
    for(int i=0;i<count;i++){
        printf("%d, ", group[i]);
    }
    return 0;
}

测试:

./test

Enter the size of the group                                                                                                                                   
5                                                                                                                                                             
Enter the 0_th number of the group:                                                                                                                           
1                                                                                                                                                             
Enter the 1_th number of the group:                                                                                                                           
2                                                                                                                                                             
Enter the 2_th number of the group:                                                                                                                           
2                                                                                                                                                             
you have already entered this number please try again:                                                                                                        
Enter the 2_th number of the group:                                                                                                                           
3                                                                                                                                                             
Enter the 3_th number of the group:                                                                                                                           
3                                                                                                                                                             
you have already entered this number please try again:                                                                                                        
Enter the 3_th number of the group:                                                                                                                           
4                                                                                                                                                             
Enter the 4_th number of the group:                                                                                                                           
5                                                                                                                                                             
1, 2, 3, 4, 5,

答案 1 :(得分:0)

如果要使用双指针,则需要这样更改函数:

void BuildGroub(int** group, int* count) {
    int i = 0;
    int j;
    printf("Enter the size of the group \n");
    scanf("%d", &*count); //I think this is redundant but works.
    while (*count != 0) {
        printf("Enter the %d number of the group:\n", i);
        j = 0;
        scanf("%d", (*group + i)); //The content of group + i 
        while ( *( *group + i) != *(*group + j)) { //the content of the content
            j++;
        }
        if (j == i) {
            i++;
            (*count)--; //The content decrement
        } else {
            printf("you have already entered this number please try again: \n");
        }
    }
}

但是您在main上遇到了一个大问题,这是因为您正在使用参数count来减小该函数内部的零。因此,当函数完成时,count的值为零,并且您什么都不打印...您需要使用内部变量进行计数来更改此值,最后确定要在{中使用的参数{1}}。