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

来源:互联网 发布:mysql 查询数据 编辑:程序博客网 时间:2024/06/05 19:34

 指针指向的字符数组长度的获取方法,不能用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;
}

原创粉丝点击