有关指针的问题,有些搞混了,看一下是怎样运行的,还有为什么电脑没有运行结果
#include<stdio.h>#include<string.h>
fun(char *w,int n)
{
char t,*s1,*s2;
s1=w; s2=w+n-1;
while(s1<s2)
{
t=*s1++;*s2--;*s2=t;
}
}
main()
{
char *p;
p="1234567";
fun(p,strlen(p));
puts(p);
}
程序代码:void fun(char *w, int n)
{
char t,*s1,*s2;
s1 = w;
s2 = w + n;//s2指向p[p+strlen(p)]=p[7]
while(s1 < s2)
{
t = *s1++; //先执行t = *s1 = 1, 然后s1++指向p[1]
*s2--; //因为s2指向p[p+strlen(p)]=p[7],p[7]是\0,所以这里执行s2--指向p[6],无需写*也行
*s2 = t; //执行更改p[6] = t = 1
}
}
程序代码: while(s1 < s2)
{
t = *s1++; //先执行t = *s1 = 2, 然后s1++指向p[2]
*s2--; //执行s2--指向p[5]
*s2 = t; //执行更改p[5] = t = 2
}
程序代码: while(s1 < s2)
{
t = *s1++; //先执行t = *s1 = 3, 然后s1++指向p[3]
*s2--; //执行s2--指向p[4]
*s2 = t; //执行更改p[4] = t = 3
}
程序代码: while(s1 < s2)
{
t = *s1++; //先执行t = *s1 = 4, 然后s1++指向p[4]
*s2--; //执行s2--指向p[3]
*s2 = t; //执行更改p[3] = t = 4
}