首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子函数中的C分段故障:我如何知道该修复什么?什么是分割错误?

子函数中的C分段故障:我如何知道该修复什么?什么是分割错误?
EN

Stack Overflow用户
提问于 2011-11-05 02:15:13
回答 4查看 712关注 0票数 1

我一直有一个分段错误,但我不知道这意味着什么,也不知道是什么导致了它(我对编程和C非常陌生)。在这个函数中,由main.c调用,我需要确定二维数组中每一行最小数的索引。

这是我的代码:

代码语言:javascript
复制
#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;
}

它正确地打印第一行,但不打印第二行。这是我的阵列:

56 7 25 89 4

-23 -56 2 99 -12

这就是我在运行这个程序时得到的结果:

x= 2,y=5f。最小数的第一个指标是:小=4小=0分段故障

这是C.谢谢您的帮助!

EN

回答 4

Stack Overflow用户

发布于 2011-11-05 02:23:15

修复typo

代码语言:javascript
复制
       for(j = 0; j < y; j++)
                         ^^
票数 5
EN

Stack Overflow用户

发布于 2011-11-05 02:19:58

分段错误意味着您正在访问不属于您的内存。

作为一个即时猜测,而不看你的代码-这是一个断断续续的错误。记住C数组是基于零的。

我很快就会看你的代码。

票数 3
EN

Stack Overflow用户

发布于 2011-11-05 02:24:22

代码语言:javascript
复制
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"
           {
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8017706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档