怎么知道一个变量在内存中所占用的实际长度

来源:百度知道 编辑:UC知道 时间:2024/05/24 14:45:26
比如有一个字符串变量

a="abcdef";

它的长度为6.

那么除strlen ,sizeof之外还有什么办法获取这个变量的长度呢?

char str[]="helloworld";
int count=0,i=0;
while(str[i]!='\0')
{
count++;
i++;
}
cout<<"the length of the string is: "<<count<<endl;

你可以自己写函数,
int p = 0;

while(a[p] != ‘\n’)
p++;

最后得到就是字符串的长度

有必要重新发明一个轮子吗?