大概应该有两处错误,我注释的地方#include <stdio.h>
#include <string.h> //使用了strcmp()函数要包含头文件
int main()
{
void sort(char *name[], int n);
void print(char *name[], int n);
char *name[] = {"I love you!", "I hate you!", "What's wrong?", "A beautiful girl!"};
int n = 4;
sort(name, n);
print(name, n);
return 0;
}
void sort(char *name[], int n)
{
int i, j, k;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(strcmp(name[k], name[j]) > 0)
{
k = j;
}
if(k != i)
{
char *temp;
temp = name[i];
name[i] = name[k];
name[k] = temp;
}
}
}
}
void print(char *name[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf("%s\n", name[i]); //这里name不加*
}
}
|