我们许多人遇到了错误‘cannot convert std::string to char[] or char* data type’。

例如:

Input : string s = "geeksforgeeks" ;Output : char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',                     'r', 'g', 'e', 'e', 'k', 's' } ;Input : string s = "coding" ;Output : char s[] = { 'c', 'o', 'd', 'i', 'n', 'g' } ;

一种方法是将字符串的内容复制到char数组。这可以借助c_str()和strcpy()函数来完成。

c_str()函数用于返回指向数组的指针,该数组包含以空终止的字符序列,该字符串表示字符串的当前值。

语法:

const char* c_str() const;

如果抛出异常,则字符串中没有任何变化。但是,当我们需要查找或访问各个元素时,可以使用strcpy()函数将其复制到char数组中。复制后,我们可以像使用简单数组一样使用它。

采用的char数组的长度不应小于输入字符串的长度。

// CPP program to convert string // to char array #include    using namespace std;   // driver code int main() {     // assigning value to string s     string s = "geeksforgeeks";       int n = s.length();       // declaring character array     char char_array[n + 1];       // copying the contents of the     // string to char array     strcpy(char_array, s.c_str());       for (int i = 0; i < n; i++)         cout << char_array[i];       return 0; } 

输出:

geeksforgeeks

另一种方法:

// CPP program to convert string // to char array #include  #include    using namespace std;   // driver code int main() {     // assigning value to string s     string s("geeksforgeeks");     // declaring character array : p     char p[s.length()];       int i;     for (i = 0; i < sizeof(p); i++) {         p[i] = s[i];         cout << p[i];     }     return 0; } 

输出:

geeksforgeeks

c 字符串数组_在C++中将字符串转换为char数组相关推荐

  1. 在Java中将字符串转换为char数组,将char数组转换为String

    Today we will learn how to convert String to a char array and then char array to String in Java. 今天, ...

  2. char数组转换成字符串_将字符串转换为char数组java –将字符串转换为char

    char数组转换成字符串 Sometimes we have to convert String to the character array in java programs or convert ...

  3. Java char转换为String,String转换为char数组

    Today we will look into java char to String program. We will also learn how to convert String to a c ...

  4. php 字符串放到数组中,在PHP中将字符串转换为数组(Converting string into array in php)...

    在PHP中将字符串转换为数组(Converting string into array in php) 我有像下面的字符串 ["Day1"]["Morning" ...

  5. lua字符串转数组_深入Lua:字符串管理

    Lua的字符串对象表示为下面结构: typedef struct TString {CommonHeader;// 字符串的子类型有两种:长字符串和短字符串// 短字符串:extra表示Lua保留字的 ...

  6. [转载] python字符串转有符号数字_在python中将字符串转换为8位带符号整数

    参考链接: Python中将十进制转换为字符串 I'm trying to patch together a motor control system using python and ctypes ...

  7. python json转字符串_在python中将json转换为字符串

    一开始我没有清楚地解释我的问题.在python中将json转换为字符串时,请尝试使用str()和json.dumps(). >>> data = {'jsonKey': 'jsonV ...

  8. java集合转字符串拼接_关于集合和字符串的互转实现方法

    今天在写项目的时候遇到一个问题,就是要把得到的一个集合转换成字符串,发现 import org.apache.commons.lang.stringutils; 有这么一个简单的方法:string s ...

  9. python字符串操作_浅谈Python 字符串特有的操作方法

    来源:(微信号:python_cat)" 正如<你真的知道Python的字符串是什么吗?>所写,Python中字符串是由Uniocde编码的字符组成的不可变序列,它具备与其它序列 ...

最新文章

  1. 一次线上 JVM 调优实践,FullGC 40 次/天到 10 天一次的优化过程
  2. 关于博客园的TrackBack功能
  3. SQL--Chapter1_Overview of SQL Server
  4. lduan HyPer-V 虚拟机服务监控及更新(十一)
  5. 异质性查询需要为连线设定_振奋人心!华东理工大学开发新型的荧光染料,为细胞成像奠定基础...
  6. geoserver发布瓦片_Geoserver2.15.1配置自带GeoWebCache 插件发布ArcGIS Server瓦片
  7. Xcode调试项目时取消弹出框提示授权
  8. drools 7.x定时器
  9. ifdef的用法_chisel 高阶用法简介--rocket-chip generator
  10. 翻译: Swift 中信号量的美妙之处
  11. 关于安装NTKO Office插件的方法
  12. jade入门与初步使用
  13. 汽车发动机电子控制技术
  14. Unity语音合成-初识有道语音合成
  15. 上顿号符号_顿号在键盘上怎么打 常见的电脑符号输入方法说明
  16. Python爬虫:博客被抄袭了还不知道?快来查查
  17. 如何选择拨号服务器!
  18. 学习Spring,看这几本书就够了
  19. 安卓笔记之ViewPager页卡
  20. Oracle数据库-第一章:select语句,数据查询操作

热门文章

  1. 数据结构-王道2017-第5章 图
  2. 关于数据结构的赋值,最好不要直接赋值,而是采用构造函数、或其它函数来赋值,这样可以方便控制---我自己!...
  3. 外观模式(三层解耦)
  4. 修改TOMCAT服务器图标为应用LOGO
  5. POJ-2400 Supervisor, Supervisee 带权值匹配+枚举所有匹配情况
  6. 讲解SQL Server定时作业job的设置方法
  7. ASP.NET2.0 ObjectDataSource的使用详解
  8. linux——select、poll、epoll
  9. mysql connector net 6.9.3_MySQL Connector/Net 6.9.3 发布 MySQL Connector/Net 6.9.3下载
  10. gitbook mysql_使用Gitbook做笔记