Know if const qualifier is used

后端 未结 4 1641
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 13:46

Is there any way in C to find if a variable has the const qualifier? Or if it\'s stored in the .rodata section?

For example, if I have this function:



        
4条回答
  •  感动是毒
    2021-01-19 14:20

    You can't differ them using the language alone. In other words, this is not possible without recurring to features specific to the compiler you're using, which is likely not to be portable. A few important remarks though:

    In the first case you COULD modify the string, but you MUST NOT. If you want a mutable string, use initialization instead of assignment.

    char *str1 = "abc"; // NOT OK, should be const char *
    const char *str2 = "abc"; // OK, but not mutable
    char str3[] = "abc"; // OK, using initialization, you can change its contents
    

提交回复
热议问题