将变量c改为一维字符数组就可以了;
或者将复制字符串那里改为c[0],因为二维字符数组c中只有一个长度为20的字符数组,它的下标是0,所以使用c[1]复制字符串会数组越界,而导致错误,所以将c[1]改为c[0]应该就可以了。
修改如下:
#include <stdio.h>
#include <string.h>
int main(void){
int i,j;
char s[5][20];
char c[1][20];
printf("请输入5个字符串:\n");
for(i=0;i<5;i++){
gets(s[i]);
}
for(i=0;i<5;i++){
for(j=i+1;j<5;j++){
if(strcmp(s[i],s[j])>0){
strcpy(c[0],s[i]); // 将s[i]中的字符串复制到二维数组c的第一个字符数组位置
strcpy(s[i],s[j]);
strcpy(s[j],c[0]); // 将维数组c的第一个字符数组位置的字符串复制到s[j]中
}
}
}
printf("这5个字符串为:\n");
for(i=0;i<5;i++){
puts(s[i]);
}
}
修改如下,供参考:
#include <math.h>
#include <stdio.h>
#define sqr(n) ((n) * (n))
typedef struct{
double x;
double y;
} Point;
double distance_of(Point pa, Point pb){
return sqrt(sqr(pa.x - pb.x) + sqr(pa.y - pb.y));
}
int main(void){
Point crnt, dest;
printf("当前地点的X坐标:");
scanf("%lf", &crnt.x);
printf("当前地点的Y坐标:");
scanf("%lf", &crnt.y);
printf("目的地点的Y坐标:");
scanf("%lf", &dest.x);
printf("目的地点的Y坐标:");
scanf("%lf", &dest.y);
printf("到目的地的距离为 %.2f。\n", distance_of(crnt, dest));
return 0;
}