作用:字符型变量用于显示单个字符
语法:char ch = 'a';
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号内只能有一个字符,不可以是字符串
示例:
- int main() {
-
- char ch = 'a';
- cout << ch << endl;
- cout << sizeof(char) << endl;
-
- //ch = "abcde"; //错误,不可以用双引号
- //ch = 'abcde'; //错误,单引号内只能引用一个字符
-
- cout << (int)ch << endl; //查看字符a对应的ASCII码
- ch = 97; //可以直接用ASCII给字符型变量赋值
- cout << ch << endl;
-
- system("pause");
-
- return 0;
- }
作用:用于表示一串字符
两种风格
C风格字符串: char 变量名[] = "字符串值"
示例:
- int main() {
-
- char str1[] = "hello world";
- cout << str1 << endl;
-
- system("pause");
-
- return 0;
- }
注意:C风格的字符串要用双引号括起来
2.C++风格字符串: string 变量名 = "字符串值"
示例:
- int main() {
-
- string str = "hello world";
- cout << str << endl;
-
- system("pause");
-
- return 0;
- }
值得注意的是,string字符串数组中的每个字符都是char类型的:
- #include
- using namespace std;
-
- int main()
- {
- string str = "abc";
- cout << typeid(str[0]).name() << endl;
- }

并且不像一般的数组,数组名指向数组首个元素的地址,string数组的数组名指向一整个string
- int main()
- {
- string str = "abc";
- cout << *(&str) << endl;
-
- system("pause");
- return 0;
- }

- int main()
- {
-
- string str = "abc";
- cout << str << endl;
- system("pause");
- return 0;
- }

原因参考:

char *str = "hello";
在类似这样定义的char的字符串指针中指针变量ps指向的是字符串的首个字符的地址!而直接输出str的话会输出整个字符串,所以我们如果想输出字符串指针的某个字符的话需要用下面这种方式!
- int main()
- {
-
- char *str = "hello";
- cout << str << endl;
- cout << *str << endl;
- cout << *(str + 1) << endl;
- system("pause");
- return 0;
- }

上述声明方式相当于:
- int main()
- {
-
- char *str;
- str = "hello";
- cout << str << endl;
- cout << *str << endl;
- cout << *(str + 1) << endl;
-
- system("pause");
- return 0;
- }

- int main()
- {
-
- char *str[] = {"Hello", "C++", "World"}; // char (*str)[] = ...
- int i;
- for (i = 0; i < 3; i++)
- cout << str[i] << endl;
- system("pause");
- return 0;
- }
- // str[0]字符串"hello"的首地址,str[0]+1:字符串"hello"第二个字符'e'的地址,str[2]=str+2:第三个字符串"world"的首地址
- // str[1]字符串"C++"的首地址
- // str[2]字符串"world"的首地址

或
- #include
- #include
- int main()
- {
- char *str[] = {"Hello", "C++", "World"};
- char **p; //指向指针的指针
- for(p=str; p
3; p++) - puts(*p); #*p为字符串首地址,*p[0]为字符串的第一个字符地址
- }
如果试图把string类型的对象直接赋给C风格的字符串的话,编译器会报错的。
- int main()
- {
- string var = "abc";
- char *ptr = var;
- cout << *ptr << endl; // correct
- system("pause");
- return 0;
- }
但是实际应用中这个问题也难以避免,很多时候我们还是需要将string类型的转化为char*来实现自定义的操作,C++标准库也为了和之前用C写的程序兼容,于是可以用string的c_str()函数(c_str():生成一个const char*指针,指向以空字符终止的数组。),更多有关c_str函数的优缺点以及替换方案查看这篇博客:c++中c_str()的用法详解_hold_on2014的博客-CSDN博客。
- int main()
- {
- string var = "abc";
- char *ptr = var.c_str(); //错误!"const char *" 类型的值不能用于初始化 "char *" 类型的实体
- cout << *ptr << endl; // correct
- system("pause");
- return 0;
- }
或者通过const_cast去掉其const属性亦可
- int main()
- {
- string var = "abc";
- char *ptr = const_cast<char *>(var.c_str());// correct
- cout << *ptr << endl; // correct
- system("pause");
- return 0;
- }
但是c_str()为了防止意外地修改string对象,返回的是const指针,所以上面这段代码是不能被编译的。正确的应该是用const指针。
- int main()
- {
- string var = "abc";
- const char *ptr = var.c_str();
- cout << *ptr << endl; // correct
- system("pause");
- return 0;
- }