简体   繁体   中英

Can we assign a string value to "const char *var" in C langauage?

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.

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