我想将两个字符串交换,用了指针,编译器无报错,但是运行起来有些问题。
#include <stdio.h>
void swap(char *x, char *y)
{
char t;
t = *x; *x = *y; *y = t;
}
int main()
{
char* s1 = "abc", * s2 = "123";
swap(s1, s2); printf("%s,%s\n", s1, s2);
return 0;
}
我想将两个字符串交换,用了指针,编译器无报错,但是运行起来有些问题。
#include <stdio.h>
void swap(char *x, char *y)
{
char t;
t = *x; *x = *y; *y = t;
}
int main()
{
char* s1 = "abc", * s2 = "123";
swap(s1, s2); printf("%s,%s\n", s1, s2);
return 0;
}
swap里循环x,y的每个字符。你这只是交换了第一个字符。
while(*x && * y)
{
t=*x。。。。;
x++;y++;
}