When to use const char * and when to use const char []
文章推薦指數: 80 %
If you do not need to modify the data, using const char text[] reduces the number of relocations in the object file, and requires fewer machine ...
Resultsfromthe2022DeveloperSurveyarenowavailable
Home
Public
Questions
Tags
Users
Companies
Collectives
ExploreCollectives
Teams
StackOverflowforTeams
–Startcollaboratingandsharingorganizationalknowledge.
CreateafreeTeam
WhyTeams?
Teams
CreatefreeTeam
Collectives™onStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
Learnmore
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
Learnmore
Whentouseconstchar*andwhentouseconstchar[]
AskQuestion
Asked
10years,8monthsago
Modified
4monthsago
Viewed
123ktimes
68
32
Iknowtheyaredifferent,IknowhowtheyaredifferentandIreadallquestionsIcouldfindregardingchar*vschar[]
Butallthoseanswersnevertellwhentheyshouldbeused.
Somyquestionis:
Whendoyouuse
constchar*text="text";
andwhendoyouuse
constchartext[]="text";
Isthereanyguidelineorrule?
Asanexample,whichoneisbetter:
voidwithPointer()
{
constchar*sz="hello";
std::cout<
延伸文章資訊
- 1C++ const char\*与char\* const_Summit_Yue的博客 - CSDN博客
C++ const char*与char* constC++里的const char*和char* const一直很容易混,其实只要理解了const到底修饰的哪个,就会豁然开朗。
- 2在C++ 中将std::string 转换为const char* - Techie Delight
这篇文章将讨论如何在C++ 中将std::string 转换为const char*。返回的指针应指向一个char 数组,该数组包含与字符串对象中存在的字符序列相同的字符序列,并在末尾附加 ...
- 3When to use const char * and when to use const char []
If you do not need to modify the data, using const char text[] reduces the number of relocations ...
- 4C++) (字串和字元常值
在Microsoft C++ 中,您可以使用字串常值來初始化非const char 或 wchar_t 的指標。 C99 程式碼中允許這個非常數初始化,但在C++98 中已被取代,並 ...
- 5[C] 每天來點字串用法(3) - from const char* to char
這次沒有拖到欸d(`・∀・)b(然而三年後拖到了) 今天帶來的不是函式,而是一個小技巧:. 由 const char* 轉成 char*. 有時候可能在將字串傳入函式 ...