Can we assign a string value directly to const char *? why memory allocation is not required here?
For example:
const char *var = "Hello";
Consider what's happening when you do this. You have a string literal "hello"
, that exists somewhere in memory. Depending on platform and implementation, it may be read-only.
When you use it in that context, the string literal decays to a pointer to the first character, which is assigned to the pointer var
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.