注册 登录
编程论坛 C语言论坛

请问一下各位大佬,这个程序的问题,字符串排序,能运行,但是没有运行结果

cokey0 发布于 2020-06-12 13:03, 1248 次点击
#include<stdio.h>
#include<string.h>
#define N 5
#define Max 80

void sorted_string(char a[][Max])
{   
    char temp[Max];
    int j,k;
    for(j=0;j<N;j++)
    {
        for(k=j+1;k<N;k++)
        {
            if(strcmp(a[j][Max],a[k][Max])>0)
            {
                strcpy(temp[Max],a[k][Max]);
                strcpy(a[k][Max],a[j][Max]);
                strcpy(a[j][Max],temp[Max]);
            }
        }
    }
}
int main()
{
    char a[N][Max];
    int i;
    for(i=0;i<N;i++)
    {
        gets(a[i]);
    }
    sorted_string(a);
    int m;
    for(m=0;m<N;m++)
    {
        puts(a[m]);
     }
    return 0;
 }
2 回复
#2
rjsp2020-06-12 13:12
            if(strcmp(a[j][Max],a[k][Max])>0)
            {
                strcpy(temp[Max],a[k][Max]);
                strcpy(a[k][Max],a[j][Max]);
                strcpy(a[j][Max],temp[Max]);
            }

这也能编译通过? strcmp/strcpy等函数参数应该是 字符数组,你不但给的是 字符,而且还全越界了。
你先将上面代码中的“[Max]”全删掉试试先。
#3
cokey02020-06-12 13:21
回复 2楼 rjsp
已解决,字符串初学,没有弄清楚,非常感谢。
1