string-literals

C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

点点圈 提交于 2019-11-26 13:44:58
I am using gnuplot to draw a graph in C++. The graph is being plot as expected but there is a warning during compilation. What does the warning mean? warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] This is the function I am using: void plotgraph(double xvals[],double yvals[], int NUM_POINTS) { char * commandsForGnuplot[] = {"set title \"Probability Graph\"", "plot 'data.temp' with lines"}; FILE * temp = fopen("data.temp", "w"); FILE * gnuplotPipe = popen ("gnuplot -persistent ", "w"); int i; for (i=0; i < NUM_POINTS; i++) { fprintf(temp, "%lf %lf \n", xvals[i],

Why allow concatenation of string literals?

旧城冷巷雨未停 提交于 2019-11-26 12:48:33
问题 I was recently bitten by a subtle bug. char ** int2str = { \"zero\", // 0 \"one\", // 1 \"two\" // 2 \"three\",// 3 nullptr }; assert( int2str[1] == std::string(\"one\") ); // passes assert( int2str[2] == std::string(\"two\") ); // fails If you have godlike code review powers you\'ll notice I forgot the , after \"two\" . After the considerable effort to find that bug I\'ve got to ask why would anyone ever want this behavior? I can see how this might be useful for macro magic, but then why is

C++ multiline string literal

北慕城南 提交于 2019-11-26 12:37:55
Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #include ing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x. Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler: const char *text = "This text is pretty long, but will be " "concatenated into just a single string. " "The disadvantage is that you have to quote " "each part, and newlines must be literal as " "usual."; The indentation doesn't matter, since it's not inside the quotes

C++ Comparison of String Literals

て烟熏妆下的殇ゞ 提交于 2019-11-26 11:08:15
问题 I\'m a c++ newbie (just oldschool c). My son asked for help with this and I\'m unable to explain it. If he had asked me \"how do I compare strings\" I would have told him to use strcmp(), but that isn\'t what is confusing me. Here is what he asked: int main() { cout << (\"A\"< \"Z\"); } will print 1 int main() { cout << (\"Z\"< \"A\"); } will also print 1, but int main() { cout << (\"Z\"< \"A\"); cout << (\"A\"< \"Z\"); } will then print 10. Individually both cout statements print 1, but

How does the lifetime work on constant strings / string literals?

女生的网名这么多〃 提交于 2019-11-26 10:03:36
问题 I read the tutorial on the official website and I have some questions on the lifetime of constant strings / string literals. I get an error when I write the following code: fn get_str() -> &str { \"Hello World\" } error: error[E0106]: missing lifetime specifier --> src/main.rs:1:17 | 1 | fn get_str() -> &str { | ^ expected lifetime parameter | = help: this function\'s return type contains a borrowed value, but there is no value for it to be borrowed from = help: consider giving it a \'static

Regular expression for a string literal in flex/lex

萝らか妹 提交于 2019-11-26 09:16:18
问题 I\'m experimenting to learn flex and would like to match string literals. My code currently looks like: \"\\\"\"([^\\n\\\"\\\\]*(\\\\[.\\n])*)*\"\\\"\" {/*matches string-literal*/;} I\'ve been struggling with variations for an hour or so and can\'t get it working the way it should. I\'m essentially hoping to match a string literal that can\'t contain a new-line (unless it\'s escaped) and supports escaped characters. I am probably just writing a poor regular expression or one incompatible with

C/C++, can you #include a file into a string literal? [duplicate]

感情迁移 提交于 2019-11-26 09:03:15
问题 This question already has an answer here: “#include” a text file in a C program as a char[] 16 answers I have a C++ source file and a Python source file. I\'d like the C++ source file to be able to use the contents of the Python source file as a big string literal. I could do something like this: char* python_code = \" #include \"script.py\" \" But that won\'t work because there need to be \\\'s at the end of each line. I could manually copy and paste in the contents of the Python code and

Concat two `const char` string literals

孤者浪人 提交于 2019-11-26 08:25:16
问题 Is it possible to concat two string literals using a constexpr ? Or put differently, can one eliminate macros in code like: #define nl(str) str \"\\n\" int main() { std::cout << nl(\"usage: foo\") nl(\"print a message\") ; return 0; } Update : There is nothing wrong with using \"\\n\" , however I would like to know whether one can use constexpr to replace those type of macros. 回答1: Yes, it is entirely possible to create compile-time constant strings, and manipulate them with constexpr

python: SyntaxError: EOL while scanning string literal

落爺英雄遲暮 提交于 2019-11-26 07:58:34
问题 I have the above-mentioned error in s1=\"some very long string............\" Does anyone know what I am doing wrong? 回答1: You are not putting a " before the end of the line. Use """ if you want to do this: """ a very long string ...... ....that can span multiple lines """ 回答2: I had this problem - I eventually worked out that the reason was that I'd included \ characters in the string. If you have any of these, "escape" them with \\ and it should work fine. 回答3: (Assuming you don't have/want

Lifetime of a string literal returned by a function

不羁的心 提交于 2019-11-26 07:47:26
问题 Consider this code: const char* someFun() { // ... some stuff return \"Some text!!\" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } In the function someFun() , where is \"Some text!!\" stored (I think it may be in some static area of ROM) and what is its scope lifetime? Will the memory pointed by retStr be occupied throughout the program or be released once the block A exits? 回答1: The C++ Standard does not say where string literals should be stored. It does