c语言二维数组指针冒泡法排序问题,我错哪啦?

来源:百度知道 编辑:UC知道 时间:2024/06/27 15:44:29
#include<stdio.h>
chenliang(int(*p)[10]);
int main(void)
{
int i,j,a[i][j],(*s)[10];
printf("please input numbers:\n");
for(i=0;i<2;i++)
for(j=0;j<10;j++)
{
scanf("%d",&a[i][j]);
printf("\n");
}
s=a;
chenliang(s);
for(i=1;i<2;i++)
for(j=0;j<10;j++)
printf("%d",a[i][j]);
return 0;
}

chenliang(int(*p)[10])
{
int i,j,n,temp; //n shi n tang bijiao
for(i=0;i<2;i++)
{
for(n=0;n<9;n++)
for(j=0;j<9-n;j++)
if(*(*(p+i)+j)>*(*(p+i)+j+1))
{
temp=*(*(p+i)+j);
*(*(p+i)+j)=*(*(p+i)+j+1);
*(*(p+i)+j+1)=temp;
}
}
}
急用啊!!!!! 就是用二维数组分别从小到大排两组10个整数的数,一定要用指针函数二维数组,老师布置的题目,急用啊!!! 我改成a[2][10]至能排出10个,为什么啊?头大了!!!

#include<stdio.h>
chenliang(int(*p)[10]);
int main(void)
{
int i,j,a[2][10],(*s)[10];
printf("please input numbers:\n");
for(i=0;i<2;i++)
for(j=0;j<10;j++)
{
scanf("%d",&a[i][j]);
}
s=a;
chenliang(s);
for(j=0;j<10;j++)
{
for(i=0;i<2;i++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}

chenliang(int(*p)[10])
{
int i,j,n,temp; //n shi n tang bijiao
for(i=0;i<2;i++)
{
for(n=0;n<9;n++)
for(j=0;j<9-n;j++)
if(*(*(p+i)+j)>*(*(p+i)+j+1))
{
temp=*(*(p+i)+j);
*(*(p+i)+j)=*(*(p+i)+j+1);
*(*(p+i)+j+1)=temp;
}
}
}

很关键的有一处错误,是这里:
int i,j,a[i][j],(*s)[10];
c语言定义数组时要求数组元素是确定的,你这里定义了a[i][j],而i,j都是变量,肯定错了。根据你的程序,这里应该是a[2][10]。这样就没有问题了。