In C + +,
char* p = "abc"; // valid in C, invalid in C++
warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
Change to the following warning disappears
char* p = (char*)"abc"; // OK
perhaps
char const *p = "abc"; // OK
Reason analysis:
When learning C or C + +, we all know that if the types of variables on both sides of the equal sign are different during the assignment operation, the compiler will perform an operation called implicit conversion to make the variables be assigned.
In the above expression, “ABC” to the right of the equal sign is an invariant constant, which is called string literal in C + +, type is const char *, and P is a char pointer. What happens if you force the assignment?That’s right. We convert the constant coercion type on the right to a pointer. As a result, we are modifying a const constant. The result of compilation will be determined by the compiler and the operating system. Some compilers will pass, some will throw exceptions, and even if they pass, they may be killed because of the sensitivity of the operating system.
This kind of operation of directly assigning string literal to pointer is considered as deprecated by developers, but because many codes have this habit in the past, it is preserved for compatibility
Read More:
- Sublime text 3 compiles and executes C/C++ programs directly
- [Solved] Ubuntu Eclipse C/C++ Error: launch failed.binary not found
- MAC: Clion configure C compiler Error: The C compiler identification is unknown
- Chinese garbled problem when running. C file in Linux
- [Solved] Linux Compile Error: error: ‘for’ loop initial declarations are only allowed in C99 mode
- Grep: How to Find All the Files Containing a String in Linux
- [Solved] Execute ./configure Error: [error]configure: error: You need a C++ compiler for C++ support.
- C Language Compilation Error: variably modified ‘* *’ at file scope
- linux C called object ‘maze’ is not a function or function pointer printf(“%d\t“, maze(i, j))
- How to Solve C++ Error: “no matching function for call to ‘atoi/atof’”
- C# implementation of TXT document to table example code
- C#: How to Use Itextsharp to Manipulate PDF Files
- Vector series in actual C + +_ To_ fit()
- C#: How to get the value or text value of the select drop-down list
- C# WPF Framework: How to Build Caliburn.Micro Quickly
- error: C compiler cannot create executables [How to Solve]
- [Solved] configure: error: no acceptable C compiler found in $PATH
- Read and write BMP image with Pure C language
- [Solved] ioctl_cfg80211.c:1130:4: error: too many arguments to function ‘cfg80211_roamed’
- [Solved] configure:error:no acceptable C compiler found in $PATH