C++ 中字符字符串定义

2020-01-03  本文已影响0人  土DOU吹雪

1. char[]、char*、 char[10]、string(#include <string>)

char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword[] = "Hello"; 
char *myword = "Hello";
char myword[6] = "Hello";
string myword = "Hello";

1234都是 char* 类型的变量(首元素地址)。
若想在 .h 文件中定义,只能采用 4、5 两种方式。

2.加 const

char* 、const char*、string 的相互转换很简单就能查到,见参考文献2。

3.static_cast、dynamic_cast、const_cast

用于 C++ 显示转换,用的时候查一下。

参考文献:
1.http://www.cplusplus.com/doc/tutorial/ntcs/
2.https://blog.csdn.net/perfumekristy/article/details/7027678
3.https://www.jianshu.com/p/5163a2678171

上一篇 下一篇

猜你喜欢

热点阅读