简体   繁体   English

C在函数中传递2d数组给出了分段错误

[英]C Passing 2d array in function gives me segmentation fault

I'm a beginner programmer and I am currently learning C. What I'm trying to do is read the dimensions of an array from the user, create the array using malloc and then pass the created array inside a function, the function should find the minimum number of every row in the array and place it in an other array which it will also return back to the main program. 我是初学程序员,我目前正在学习C.我要做的是从用户读取数组的维度,使用malloc创建数组然后在函数内传递创建的数组,该函数应该找到数组中每行的最小数量,并将其放在另一个数组中,它也将返回到主程序。 However as soon as the array is passed into the function and I try to use it I'm getting a segmentation fault(core dumped) error. 但是,只要数组传递给函数并且我尝试使用它,我就会收到分段错误(核心转储)错误。 I know in which line the error is but I cannot locate the mistake I have made. 我知道错误在哪一行,但我找不到我犯的错误。 I would really appreciate it if you guys helped me out a little bit. 如果你们帮我一点点,我真的很感激。 Here is the code: 这是代码:

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

int *pinfun(int **pin,int sizex,int sizey);

void main(void){
    int x,y,i,j;
    int **dipin;
    int *newpin;
    printf("Parakalw eisagetai tis diastaseis x,y tou pinaka:");
    scanf("%d %d",&x,&y);
    dipin =(int**) malloc(x*sizeof(int*));
        for(i=0;i<x;i++){
                *(dipin+i)=(int*) malloc(y*sizeof(int));
        }
        for(i=0;i<x;i++){
                for(j=0;j<y;j++){
                        printf("Parakalw eisagetai tin (%d,%d) timi tou pinaka:",i,j);
                        scanf("%d",((dipin+i)+j));
                }
        }
    newpin= pinfun(dipin,x,y);
    for(i=0;i<x;i++){
        printf("H elaxisti timi tis %dis grammis einai: %d",i,*pinfun);
    }
}

int *pinfun(int **pin,int sizex,int sizey){
    int i,j,min;
        int* retpin=malloc(sizeof(int)*sizex);
        for(i=0;i<sizex;i++){
                min=**pin;
                for(j=0;j<sizey;i++){
                        if(*(*(pin+i)+j) < min){
                            min =*(*(pin+i)+j);
                        }
                }
        *(retpin + i) = min;
        }
    return retpin;
}

The code has been been fixed. 代码已修复。 The mistake was in the scanf("%d",((dipin+i)+j)); 错误在于scanf(“%d”,((dipin + i)+ j)); line . 线。 By switching it to scanf("%d", (*(dipin+i)+j)) it worked like a charm. 通过将其切换为scanf(“%d”,(*(dipin + i)+ j)),它就像一个魅力。 Thank you for your time. 感谢您的时间。

The code has been been fixed. 代码已修复。 The mistake was in: 错误在于:

scanf("%d",((dipin+i)+j));

By switching it to: 通过切换到:

scanf("%d", (*(dipin+i)+j)) 

it worked like a charm. 它就像一个魅力。 Thank you for your time. 感谢您的时间。

Of course there were also a few other typos that were mentioned but that was what was causing the segmentation fault. 当然还有一些其他的拼写错误被提及,但那是造成分段错误的原因。

for(j=0;j<sizey;i++)

I haven't gone through your whole code but this looks suspicious 我没有完成整个代码,但这看起来很可疑

it shoule be 它应该是

for(j=0;j<sizey;j++)

It would be good to make your code readble like. 让你的代码像是一样可读。

using 运用

pin[i] = *(p+i);

pin[i][j] = *(*(pin+i)+j);

Fix your printf() also as suggested by Cool Guy. 按照Cool Guy的建议修复你的printf()。

printf("H elaxisti timi tis %dis grammis einai: %d",i,newpin[i]);

Change 更改

for(j=0;j<sizey;i++)

To

for(j=0;j<sizey;j++)

And

printf("H elaxisti timi tis %dis grammis einai: %d",i,*pinfun);

To

printf("H elaxisti timi tis %dis grammis einai: %d",i,*(newpin+i));

Or 要么

printf("H elaxisti timi tis %dis grammis einai: %d",i,newpin[i]);

to make it easier to understand 使其更容易理解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM