我一直有一个分段错误,但我不知道这意味着什么,也不知道是什么导致了它(我对编程和C非常陌生)。在这个函数中,由main.c调用,我需要确定二维数组中每一行最小数的索引。
这是我的代码:
#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.谢谢您的帮助!
发布于 2011-11-05 02:23:15
修复typo
for(j = 0; j < y; j++)
^^发布于 2011-11-05 02:19:58
分段错误意味着您正在访问不属于您的内存。
作为一个即时猜测,而不看你的代码-这是一个断断续续的错误。记住C数组是基于零的。
我很快就会看你的代码。
发布于 2011-11-05 02:24:22
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"
{https://stackoverflow.com/questions/8017706
复制相似问题