69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
void swap(long *s,long *t)
{
long tmp;
tmp=*s;
*s=*t;
*t=tmp;
}
void main(void)
{
char *s="hellosadfsdf",*t="howareyou";
printf("s address is %x t address is %x\n",s,t);
swap(&s,&t);
printf("s address is %x t address is %x\n",s,t);
puts(s);
puts(t);
getchar();
}
这样不知可否帮助贴主理解,首先两个字符串都在内存中,并且s和t都指向两个字符串的首地址,s和t中保存的是两个地址的值,我们做的不是交换两部分内存中的数据 而是交换两个地址的值