鱼C论坛

 找回密码
 立即注册
查看: 3501|回复: 9

求助

[复制链接]
发表于 2022-5-26 10:17:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<string.h>

#define NUMBER 5
#define NAME_LEN 64

void swap_str(char *s1, char *s2)
{
    char *temp = s1;
    s1 = s2;
    s2 = temp;
}

void swap_int(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;

}

void sort(int num[], char str[][NAME_LEN], int n)
{
    int i, j;

    for(i = 0; i < n -1; i++)
        for(j = 0; j < n - 1 - i; j++)
        {
             if(num[j] >num[j + 1])
                swap_int(&num[j], &num[j + 1]);
                swap_str(str[j], str[j + 1]);
        }


}

int main()
{
    int i;
    int height[] = {178, 175, 173, 165, 179};
    char name[][NAME_LEN]={"Sato","Sanaka","Takao","Mike","Masaki"};

    for(i = 0; i < NUMBER; i++)
        printf("%2d : %-8s%4d\n", i + 1, name[i], height[i]);

    sort(height, name, NUMBER);

    puts("\n按身高进行升序排列。");
    for(i = 0; i < NUMBER; i++)
        printf("%2d : %-8s%4d\n", i + 1, name[i], height[i]);


    return 0;
}



第一个交换字符串的函数,我知道用strcpy实现肯定可以,我想请问一下用指针实现不可以吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-26 10:57:33 | 显示全部楼层
可以的
#include <stdio.h>

void swap_str(char *s1, char *s2)
{
    char *temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

int main()
{
    char * a="99";
    char * b="Abc";
    printf("before: a=%s   b=%s  \n",a,b);
    swap_str(&a,&b);
    printf("after: a=%s   b=%s  \n",a,b);
    printf("OK\n");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-26 11:19:21 | 显示全部楼层

老哥在你的编译环境能运行是吗?我这运行不了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-26 11:20:23 | 显示全部楼层
hpdie 发表于 2022-5-26 11:19
老哥在你的编译环境能运行是吗?我这运行不了

运行不了  还是编译不了  还是无法生成exe  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-26 11:22:54 | 显示全部楼层
hpdie 发表于 2022-5-26 11:19
老哥在你的编译环境能运行是吗?我这运行不了
#include <stdio.h>

void swap_str(char *s1, char *s2)
{
    char *temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

int main()
{
    char * a="我是第一个字符串";
    char * b="me is the second chars";
    printf("交换之前: a=%s   b=%s  \n",a,b);
    swap_str(&a,&b);
    printf("after: a=%s   b=%s  \n",a,b);
    printf("OK\n");
}

/*
样例输出:
    PS D:\wp> ./ct8
        交换之前: a=我是第一个字符串   b=me is the second chars  
        after: a=me is the second chars   b=我是第一个字符串  
        OK
    PS D:\wp> 
*/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-26 11:44:44 | 显示全部楼层

void swap_str(char *s1, char *s2)
{
    char *temp = *s1;    //s1的解引用直接赋值给指针temp报错,
    *s1 = *s2;  //这里是只会交换两个字符串的首字符吗?
    *s2 = temp; // 把指针赋值给解引用也是报错
}

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
D:\software\codeblocks\程序\mj10.cpp||In function 'void swap_str(char*, char*)':|
D:\software\codeblocks\程序\mj10.cpp|5|error: invalid conversion from 'char' to 'char*' [-fpermissive]|
D:\software\codeblocks\程序\mj10.cpp|7|error: invalid conversion from 'char*' to 'char' [-fpermissive]|
D:\software\codeblocks\程序\mj10.cpp||In function 'int main()':|
D:\software\codeblocks\程序\mj10.cpp|12|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
D:\software\codeblocks\程序\mj10.cpp|13|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
D:\software\codeblocks\程序\mj10.cpp|15|error: cannot convert 'char**' to 'char*' for argument '1' to 'void swap_str(char*, char*)'|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-26 12:11:57 From FishC Mobile | 显示全部楼层
hpdie 发表于 2022-5-26 11:44
void swap_str(char *s1, char *s2)
{
    char *temp = *s1;    //s1的解引用直接赋值给指针temp报错, ...

我二楼的第15行代码  你有改动吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-26 15:03:13 | 显示全部楼层
wp231957 发表于 2022-5-26 12:11
我二楼的第15行代码  你有改动吗

没改,一行都没改,传的是指针,接收的也是指针我知道、老哥你用的什么编译器啊?我下一下运行试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-26 15:05:00 From FishC Mobile | 显示全部楼层
hpdie 发表于 2022-5-26 15:03
没改,一行都没改,传的是指针,接收的也是指针我知道、老哥你用的什么编译器啊?我下一下运行试试

gcc11.x
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-26 18:08:19 | 显示全部楼层

老哥那我的程序问题出在哪里啊?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-8 02:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表