要交换的是str1与str2的指针
却交换了str1与str2
#include <stdio.h>
void swap(char *p1, char *p2);
int main(void) {
char str1[100]="This is my String.";
char str2[100]="Another String.";
char *p1=str1;
char *p2=str2;
printf("1---Address:p1=%p,p2=%p\n",p1,p2);
printf("Before swap:\np1:%s\np2:%s\n",p1,p2);
swap(p1,p2);
printf("4---Address:p1=%p,p2=%p\n",p1,p2);
printf("After swap:\np1:%s\np2:%s\n",p1,p2);
getchar();
return 0;
}
void swap(char *p1, char *p2) {
char *pt=p1;
p1 = p2;
p2 = pt;
}
1---Address:p1=0x22ccc0,p2=0x22cc50
Before swap:
p1:This is my String.
p2:Another String.
4---Address:p1=0x22ccc0,p2=0x22cc50
After swap:
p1:This is my String.
p2:Another String