小编And*_*rew的帖子

我怎样才能使字符串数组用交换函数交换它的组件?

问题是这段代码不会交换这两个字符串.我是编程的新手,但我可以说问题是交换功能,但我不知道如何解决它.

我试图在交换中添加strcpy而不是"="但是没有用.

#include <stdio.h>
#include <stdlib.h>

void swap(char *t1, char *t2) {
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}
int main() {
    char *s[2] = {"Hello", "World"};
    swap(s[0], s[1]);
    printf("%s\n%s", s[0], s[1]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c swap pass-by-value function-call

3
推荐指数
1
解决办法
64
查看次数

标签 统计

c ×1

function-call ×1

pass-by-value ×1

swap ×1