结果非预期(代码没写全是因为:)
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]);
}
}
}
你怎么判断没有调用函数的呢?
#include <string.h>
#include <stdio.h>
void compare(char p[], int s)
{
void temp(char a, char* b);
int i, a, q;
for (i = 0; i < s - 1; i++)
{
q = i;
for (a = i + 1; a < s; a++)
{
if (strcmp((p + q), *(p + a)) > 0)
q = a;
}
if (q != a)
temp((p + q), (p + a));
}
}
void temp(char a, char* b)
{
char* t;
t = a;
a = b;
b = t;
}
void 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]);
}
}