【问题标题】:segmentation fault (core dumped) gcc ubuntu分段错误(核心转储)gcc ubuntu
【发布时间】:2020-09-08 02:20:07
【问题描述】:

我试图制作一个简单的函数来制作一组用户输入的数字,使用指针的指针,但我不断收到此错误,并且很难判断问题出在哪里,如果有任何其他选项可以使用某些东西else 告诉我问题出在哪里,而不是这个奇怪的错误。

#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;
}

【问题讨论】:

  • @FiddlingBits 抱歉
  • 你不需要双指针。数组的名称是第一个元素的地址。它是一个地址。您只需将groupptr 传递给您的函数,将其定义为接收int*,而不是int**。抱歉,但我不明白您在获得每个号码后要做什么。
  • @SuperG280 我知道它只是尝试使用双指针我并没有真正了解多个指针的工作原理,我什至没有看到这段代码导致核心转储的问题跨度>
  • @SuperG280 什么都只打印我得到的东西,它唯一检查我之前是否已经输入过数字,所以它打印不同的数字

标签: c arrays pointers gcc


【解决方案1】:

对于这个问题,你不需要使用双指针。如果你想学习如何使用双指针,你可以 google 那里有大量的例子供你参考,例如,Double Pointer (Pointer to Pointer) in C

BuildGroub 中,您减少了count 指针

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

,但是在while循环的情况下,你比较count指针指向的值。好像很奇怪。

while(*count != 0)

即使您将count-- 更改为(*count)--,当您退出while 循环时,它也会将您输入的元素数量减少到0,然后在main 函数中:

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,

【讨论】:

    【解决方案2】:

    如果你想使用双指针,你需要像这样改变你的函数:

    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 值为零并且您不打印任何内容...您需要更改它,使用内部变量进行计数,最后设置要在main 中使用的参数.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2021-06-03
      相关资源
      最近更新 更多