怎么运行的希望解释一下

#include <stdio.h> int fun(char *s) { char *p=s; while (*p) p++ ; return (p-s) ; }

int main ( ) { char *a="abcdef" ; printf("%d\n",fun(a)) ; }

fun函数是统计字符串长度,s为字符串的开始位置,p为字符串的结束位置。相减就是长度。

供参考:

#include <stdio.h>
int fun(char *s)
{
    char *p=s; //指针p 指向字符串 s首地址
    while (*p) p++ ; //循环,P指针指向字符串尾部结束符处
    return (p-s) ; //末地址 减去 首地址 = 字符串长度值
}

int main ( )
{
   char *a="abcdef" ;
   printf("%d\n",fun(a)) ;//输出字符串长度
}

计算长度啊 while(*p) p++;循环就是循环查找字符串结束符'\0',找到结束符位置后,用该位置的地址减去字符串的首地址,得到的值就是字符串字符的个数,也就是长度啦。