当前位置:网站首页>Char[], char *, conversion between strings
Char[], char *, conversion between strings
2022-06-23 01:14:00 【ppipp1109】
char [] And char * Between
char [] turn char *: Assign values directly
// char[] turn char *
char str[] = "lala";
char *str1 = str;
cout << str1 << endl;
char * turn char[]: Character copy implementation , You cannot assign values
// char * Convert to char []
const char *st = "hehe";
char st1[] = "lalalala";
strncpy(st1, st, strlen(st) + 1); // Pay attention to add 1 operation
// tp = temp; // error , Can't achieve
cout << st1 << endl;
char And const char Between
const char turn char : Copy implementation , Cannot assign
// const char * turn char *
const char *st = "lala";
// Direct assignment is not allowed
//char *st1 = st; // ( No compiler error is allowed )
//cout << st1 << endl;
// In addition, open up space , Copy the characters one by one
char *ncstr = new char[strlen(st) + 1];
strcpy(ncstr, st);
cout << ncstr << endl;
char turn const char : Assign values directly
// char * turn const char *
char *st = "hehe"; // ( Compile prompt warning )
const char *st1 = st;
cout << st1 << endl;
char * And string Between
char * turn string:1) Direct assignment ;2) Construct transformation to realize
// char* Convert to string
// ( Be careful , Definition char * Variable , And assign values directly , Best defined as const Variable , Otherwise, the compiler warns )
const char *st = "hello";
// Assignment conversion
string st1 = st;
cout << st1 << endl;
// Structural transformation
string s1(st, st + strlen(st));
cout << s1 << endl;
// change const char * A variable's value
st = "lalala";
cout << st << endl;
string turn char *: Assignment operation ( Pay attention to type conversion )
// string turn char *
string st = "My test";
//char *st1 = st; // Different types of errors
//char *st1 = st.c_str(); // Different types of errors
char *st1 = const_cast<char *>(st.c_str()) ;
cout << st1 << endl;
char[] And string Between
char [] turn string:1) Direct assignment ;2) Construct transformation to realize
// char[] Convert to string
char st[] = "hello";
// Direct assignment to achieve
string st1 = st;
cout << st1 << endl;
// Construct implementation
string st2(st, st + strlen(st));
cout << st2 << endl;
string turn char[]: Copy implementation , It cannot be assigned directly
// string turn char []
string ts = "My test1";
//char ts1[] = ts; // error
//char ts1[] = const_cast<char *>(ts.c_str()); // error
char ts1[] = "lalallalalaaaa";
strncpy(ts1, ts.c_str(), ts.length() + 1); // Be careful , Be sure to add. 1, Otherwise, there is no assignment '\0'
cout << ts1 << endl;
return 0;
summary
involves char [] Character array and other types conversion , Generally, copying is required , It is not possible to assign values directly .char [] and char * By constructing new string Complete their pairs string Transformation . Involving char * transformation , Note that the types are consistent , At the same time pay attention to const Use .
边栏推荐
- Add expiration time for localstorage
- Figure what are the uses and applications of neural networks?
- 魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
- Installing MySQL for Linux
- SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application
- cadence SPB17.4 - 中文UI设置
- LINQ 查询
- SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
- Is it safe for CICC securities to open an account? What is its relationship with CICC?
- How to calculate the position of gold ETF
猜你喜欢

OSPF experiment in mGRE environment

Figure what are the uses and applications of neural networks?

Typecho仿盧松松博客主題模板/科技資訊博客主題模板

62. 不同路径
3 big questions! Redis cache exceptions and handling scheme summary

數據庫中數據的儲存結構和方式是什麼?

Tidb monitoring upgrade: a long way to solve panic

SAP mm me27 create intra company sto order

How to get started with machine learning?

【滑动窗口】leetcode992. Subarrays with K Different Integers
随机推荐
基于深度学习的视觉目标检测技术综述
[Title Fine brushing] 2023 Hesai FPGA
Yyds dry goods counting tail recursion is better than recursion
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
Is it reliable to get new debts? Is it safe?
Flink synchronizes MySQL data to es
"Hearing" marketing value highlights, Himalaya ushers in a new situation
C serializabledictionary serialization / deserialization
C language student achievement ranking system
cadence SPB17.4 - 中文UI设置
Typecho仿盧松松博客主題模板/科技資訊博客主題模板
SFOD:无源域适配升级优化,让检测模型更容易适应新数据
Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
Charles garbled code problem solving
C#. Net universal database access encapsulation classes (access, sqlserver, Oracle)
Learn the specific technical learning experience of designing reusable software from the three levels of class, API and framework
Similar to attention NLP
07 project cost management
时间复杂度
E-R diagram