问题遇到的现象和发生背景
结果非预期(代码没写全是因为:)
问题相关代码,请勿粘贴截图
void compare(char *p[], int s)
char string_s[3] = {"hello", "apple", "display"};
compare(string_s , 3);//为什么程序没有调用此函数
运行结果及报错内容
我的解答思路和尝试过的方法
使用形参*p或者p[]来接受指针数组首地址
我想要达到的结果
正常输出,期望有完整代码
结果非预期(代码没写全是因为:)
void compare(char *p[], int s)
char string_s[3] = {"hello", "apple", "display"};
compare(string_s , 3);//为什么程序没有调用此函数
使用形参*p或者p[]来接受指针数组首地址
正常输出,期望有完整代码
代码修改如下:
下面的代码是字符串从小到大排序,如果想要从大道小排序,只需要把if(strcmp(p[j],p[minindex])<0)改成 if(strcmp(p[j],p[minindex])>0)即可。
运行结果如下:
代码:
#include <stdio.h>
#include <string.h>
void compare(char *p[], int s);
void temp(char** a, char** b);
int main()
{
int i;
char *string_s[3] = {"hello", "apple", "display"};
printf("就绪: \n");
compare(string_s , 3);//为什么程序没有调用此函数
//输出
for(i=0;i<3;i++)
printf("%s ",string_s[i]);
return 0;
}
void temp(char** a, char** b)
{
char* t;
t = *a;
*a = *b;
*b = t;
}
void compare(char *p[], int s)
{
int i,j;
int minindex;
for (i=0;i<s-1;i++)
{
minindex = i;
//找出第i小的数所在的位置
for(j=i+1;j<s;j++)
{
if(strcmp(p[j],p[minindex])<0)
minindex = j;
}
//将第i小的数放在第i个位置
if (i != minindex)
{
temp(&p[i],&p[minindex]);
}
}
}