指针指向的字符数组长度的获取方法

 指针指向的字符数组长度的获取方法,不能用sizeof,因为用sizeof(指针),得到指针长度为4

应该用strlen()函数。

注意:如果sizeof(字符数组名),可以用上一篇的方法得到。

小例子:

#include <iostream.h>
#include <string.h>

int num(char *ptr)
{
 int bb = strlen(ptr);
 return bb;
}

int main()
{
 char *p= new char[100];
 p = "string";
 int b = num(p);
 cout<<b<<endl;
 return 0;
}


版权声明:本文为wm_cindy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。