#include
#include
using namespace std;
int main(void)
{
char *str = new char[100];
strcpy(str,"hello imooc");
cout << "*str";
delete[] str;
system("pause");
return 0;
}
如果是输出str内容的话或者是str字符串长度的话可以用以下方式:
#include "iostream"
#include "string"
#include "vector"
using namespace std;
int main(void)
{
char *str = new char[100];
strcpy(str,"hello imooc");
cout <<"str字符串内容是: " << str << endl;
cout <<"str字符串长度是: " << strlen(str) << endl;
delete[] str;
return 0;
}
如果你仅仅只有str这个字符指针的话,你想要获取到数组整体大小,很遗憾,这个str指针做不到,你要把str定义为数组类型的才行:
#include "iostream"
#include "string"
#include "vector"
using namespace std;
int main(void)
{
char str[100];
strcpy(str,"hello imooc");
cout <<"str字符串内容是: " << str << endl;
cout <<"str字符串长度是: " << strlen(str) << endl;
cout<<"str数组长度: "<< sizeof(str)/sizeof(char) <<endl;
return 0;
}
cout << "*str";
输出的是
*str
因为有引号。
去掉引号,输出的是hello imooc。
要输出100,需要用sizeof
cout << "*str";中 "*str"是字符串,
所以输出的是
*str
因为有引号。
去掉引号,输出的是hello imooc。
要输出100,需要用sizeof
cout << "*str";中 "*str"是字符串,
所以输出的是
*str
因为有引号。
去掉引号,输出的是hello imooc。
要输出100,需要用sizeof
"*str" 会当做字符串输出,变量不需要加引号