c语言中将字符串进行“字典排序”之后输出的问题

#include<stdio.h>#include<string.h>intmain(){voidsort(char*s[]);char*a[10];inti;for(i... #include<stdio.h>
#include<string.h>
int main()
{
void sort(char *s[]);
char *a[10];
int i;
for (i = 0; i < 10; i++)
scanf("%s", a[i]);
sort(a);
for (i = 0; i < 10; i++)
printf("s\n", a[i]);
return 0;
}
void sort(char *s[])
{
char t[10];
int i, j;
for (i = 0; i<9; i++)
for (j = 0; j<9 - i; j++)
if (strcmp(s[j], s[j + 1])>0)
{
strcpy(t, s[j]); strcpy(s[j], s[j + 1]); strcpy(s[j + 1], t);
}
}
这样写为什么会运行错误呢
展开
 我来答
ccccccc12
2016-12-22 · TA获得超过428个赞
知道小有建树答主
回答量:378
采纳率:0%
帮助的人:308万
展开全部

同学你好,你定义的指针数组,没有向系统申请空间

请用malloc向系统申请空间在赋值

#include<malloc.h>
for (i = 0; i < 10; i++){
a[i]=(char*)malloc(1024);
scanf("%s", a[i]);
}

改下赋值部分

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式