如何进行(指针数组)的函数参数传递,如下,以下程序该如何改正才能正常运行

问题遇到的现象和发生背景

结果非预期(代码没写全是因为:)

img

问题相关代码,请勿粘贴截图

void compare(char *p[], int s)
char
string_s[3] = {"hello", "apple", "display"};
compare(string_s , 3);//为什么程序没有调用此函数

运行结果及报错内容

img

我的解答思路和尝试过的方法

使用形参*p或者p[]来接受指针数组首地址

我想要达到的结果

正常输出,期望有完整代码

代码修改如下:

下面的代码是字符串从小到大排序,如果想要从大道小排序,只需要把if(strcmp(p[j],p[minindex])<0)改成 if(strcmp(p[j],p[minindex])>0)即可。
运行结果如下:

img

代码:

#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]);
}
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632