#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;
}