用gets输入字符串有问题,格式不太对,直接gets(数组名)输入会报错
#include<stdio.h>
#include<string.h>
void main()
{
void xzz(char *arr[],int n);
char *zxx[50];//={"Have you got a table for two, please","Have you booked a table","Have you made a reservation","smoking or non-smoking","Would you like something to drink"
//,"Would you like to see the menu","Excuse me, could I see the menu, please","Are you ready to order","How would you like to pay","For here or to go"};
int i;
printf("输入十个字符串: ");
for(i=0;i<10;i++)
gets(*(zxx+i));
//scanf("%s",zxx[i]);
xzz(zxx,10);
printf("输出十个排序后的字符串: ");
for(i=0;i<10;i++)
{
printf("%s ",zxx[i]);
}
}
void xzz(char *arr[],int n)
{
char *p;
int k,i,j;//k作为判断最小值
for(i=0;i<n-1;i++)
{k=i;
for(j=1;j<n;j++)
if(strcmp(arr[i],arr[j]) > 0)k=j;//前者大于后者,将后者赋给k
if(k!=i)
{
p=arr[i];
arr[i]=arr[k];
arr[k]=p;
}
}
}
你的字符串数组char *zxx[50];
只定义了指针,没有给它申请内存,所谓的野指针,当然会出错的
对新手,尽量用数组代替指针,你要10个串的话,可用
char zxx[10][50];
指针的话,你也写反了,应该是
char *zxx[10];
然后在循环中,gets前加
zxx[i]=(char *)malloc(50); //为每个字串申请50个字符空间
另外你的排序也不正确,你的算法有问题的