简体   繁体   English

将字符串转换为 char* 数组

[英]Convert a string to char* array

I have sentence of char*我有一句 char*

char *s1 = "coding hero";

How do I Convert to我如何转换为

char *s1 = {coding hero};

You wouldn't convert a char * (string) to a char* array but rather to a char array:您不会将char * (string) 转换为char*数组,而是转换为char数组:

char s1[] = "coding hero";

A char * (string) is similar to a char array for most purposes, but not identical.在大多数情况下, char * (字符串)类似于char数组,但不完全相同。 There are slightly different rules that apply to the two of them.两者适用的规则略有不同。

If desired, you can also write:如果需要,您还可以编写:

char s1[] = { 'c', 'o', 'd', 'i', 'n', 'g', ' ', 'h', 'e', 'r', 'o', '\0' };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM