方法一:
利用指针特性进行运算
#include <stdio.h>#include <string.h>#include <windows.h>//计算字符串长度不用string函数int strl�仯����,�仯���(char* p,int sz){int i;int count = 0;for (i=0;i<sz;i++){printf("%d\n", *p);if (*p != '\0'){count =count + 1;p ++;}else{p ++;} }return count; }int main(){char ch[] = { 'a','b','\0' };int sz = sizeof(ch) / sizeof(ch[0]); int a=strl(ch,sz); printf("%d \n", a);}
方法二:
两个指针相减得位数:
#include <stdio.h>#include <string.h>int strl(char* p, int sz){char* start = p;char* end = p;while (*end != '\0'){end = end + 1;}return end- start;}int main(){char ch[] = { 'a','b','c','\0' };int sz = sizeof(ch) / sizeof(ch[0]);//测试/*printf("%d \n", sz);*/int b = strl(ch, sz);printf("ch数组字符串长度:%d\n", b);}