简体   繁体   English

子功能中的C分段故障:如何知道要解决的问题? 什么是细分错误?

[英]C Segmentation Fault in Sub-function: How do I know what to fix? What is a segmentation fault?

I keep getting a segmentation fault, but I'm not sure what that means or how to tell what is causing it (I'm very new to programming and C). 我一直遇到分段错误,但是我不确定这是什么意思或如何确定是什么原因造成的(我对编程和C语言非常陌生)。 In this function, called by main.c, I need to determine the index of the smallest number in eacg row of a two-dimentional array. 在由main.c调用的此函数中,我需要确定二维数组的eacg行中最小数字的索引。

Here is my code: 这是我的代码:

#include "my.h"

void findfirstsmall (int x, int y, int** a)
{
    int i;
    int j;
    int small;  

    small = y - 1;


printf("x = %3d, y = %3d\n", x, y);                      //trying to debug


    printf("f.  The first index of the smallest number is: \n");
    for(i = 0; i < x; i++)
        {
           for(j = 0; j < y; i++)          <---------- needs to be j, for any future readers
               {
                  if(a[i][small] > a[i][j])
                        small = j;
printf("small = %3d\n", small);                          //trying to debug
               }
           printf("Row: %4d, Index: %4d\n", i, small);
           small = y - 1;
           printf("\n");
        }
    printf("\n");
    return;
}

It prints correctly for the first row, but not the second. 它在第一行正确打印,但在第二行不正确。 This is my array: 这是我的数组:

56 7 25 89 4 56 7 25 89 4
-23 -56 2 99 -12 -23 -56 2 99 -12

This is what I am getting when I run the program: 这是我运行程序时得到的:

 x = 2, y = 5 f. The first index of the smallest number is: small = 4 small = 0 Segmentation fault 

This is in C. Thanks in advance for the help! 这是C语言。在此先感谢您的帮助!

Fix the typo : 解决typo

       for(j = 0; j < y; j++)
                         ^^

A segmentation error means that you are accessing memory that you do not own. 分段错误表示您正在访问您不拥有的内存。

As a instant guess without looking at your code - it is an off-by-one-error. 无需查看您的代码即可立即猜测-这是一个错误的提示。 Remember C arrays are zero based. 请记住,C数组基于零。

I will look at your code more soon. 我将尽快查看您的代码。

printf("f.  The first index of the smallest number is: \n");
for(i = 0; i < x; i++)
    {
       for(j = 0; j < y; i++) // my guess is that you increment "i" instead of "j"
           {

Note that there is a difference between a two-dimensional array and an array of pointers (see this question ). 请注意,二维数组和指针数组之间有区别(请参阅此问题 )。 Depending on what you're doing in main() , this could be your problem. 根据您在main() ,这可能是您的问题。 For example, the following would not work with the function as is, since it passes a pointer to memory containing an array of arrays: 例如,以下代码不能按原样使用该函数,因为它会将指针传递给包含数组数组的内存:

int arr[2][5] = { {  56,   7,  25,  89,   4 },
                  { -23, -56,   2,  99, -12 } };
findfirstsmall (2, 5, arr);

However, this would be ok, since it passes an array of pointers to the start of each sub-array of arr : 但是,这没关系,因为它将指针数组传递到arr的每个子数组的开头:

int arr[2][5] = { {  56,   7,  25,  89,   4 },
                  { -23, -56,   2,  99, -12 } };
int *tmp[2];
tmp[0] = &arr[0][0];
tmp[1] = &arr[1][0];
findfirstsmall (2, 5, tmp);

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

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