我无法找到指针数组的长度.假设我有:
char array[40] = "hello"; // size 40
int length = sizeof array / sizeof array[0]; // no problem returns 40
//如何获取数组的长度,只有指向该数组中第一个元素的指针?
char* pchar = array;
//如果
std::strlen(pchar); // this returns the length of 5... i want length 40
//如果
int count = 0;
while(true)
{
if(*(pchar + count) == '\0') // returns 5...
break;
count++;
}
如何让它从一个指针返回数组中第一个元素的长度40?
我发现我可以做到这一点.
int count = 0;
while(true)
{
if(*(pchar + count) == '\0' && *(pchar + count + 1) != '\0')
break;
count++;
}
这返回39,这是好的,但我觉得在某些情况下,这可能是错误的.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)