string-literals

Why do I get a compiler warning for converting a string literal to a char*, is it bad?

試著忘記壹切 提交于 2019-12-02 01:32:10
问题 So the compiler tells me this is a deprecated conversion from a string-literal to char*: char* myString = "i like declaring strings like this"; Should I be worried about this? Is this the wrong way to do this? I need to pass myString to a function that accepts a char* , who should I properly initialize the char* without this conversion? 回答1: It shouldn't even compile. If you need to pass it to function that you are sure won't change the string you need to use const cast, its one of its

PHP dynamically reference variable in string literal

走远了吗. 提交于 2019-12-02 01:15:28
I have multiple PHP variables in the form $number1, $number2, $number3 and so on... I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex: for($i = 1; $i <= 10; $i++) { //The first number to be printed should be the value from //$number1 not $number concatenated to $i //here are some of the strings I tried: echo "$number$i"; echo "{$number}$i"; echo "{$number}{$i}"; } This should do it: echo ${"number{$i}"}; But why not use arrays instead? Having $number[$i] is much more readable...

What's the advantage of having multi-line & single-line string literals in python?

女生的网名这么多〃 提交于 2019-12-02 01:11:52
I know the triple quote strings are used as docstrings, but is there a real need to have two string literals? Are there any use case when identifying between single-line & multi-line is useful. in Clojure we have 1 string literal, is multi-line and we use it as docstring. So why the difference in python? The advantage of having to be explicit about creating a multi-line string literal is probably best demonstrated with an example: with open("filename.ext) as f: for line in f: print(line.upper()) Of course, any decent syntax-highlighting editor will catch that, but: It isn't always the case

Is it “bad practice” to use tab characters in string literals?

泄露秘密 提交于 2019-12-01 23:50:20
问题 As a follow-up on Is it mandatory to escape tabulator characters in C and C++? (do note I'm not the author of said question). I've learned such code is considered "bad practice". The comments seem to be suggesting the same thing. However, for some reason the standard allows this trickery so somebody must either found no harm in it or has a use-case for it. Is not escaping tabulator characters widely accepted as "bad practice"? 回答1: Using tabs in litterals instead of escaping them ( '\t' ,"\t"

Why do I get a compiler warning for converting a string literal to a char*, is it bad?

点点圈 提交于 2019-12-01 22:02:14
So the compiler tells me this is a deprecated conversion from a string-literal to char*: char* myString = "i like declaring strings like this"; Should I be worried about this? Is this the wrong way to do this? I need to pass myString to a function that accepts a char* , who should I properly initialize the char* without this conversion? It shouldn't even compile. If you need to pass it to function that you are sure won't change the string you need to use const cast, its one of its correct uses: functionName(const_cast<char *>("something")); Or if you don't want the const cast, you can copy the

pointers and string literals

隐身守侯 提交于 2019-12-01 21:24:26
Many a times I have seen the following statements: char* ch = "Hello" cout<<ch; The output I get is " Hello ". I know that ch points to the first character of the string "Hello" and that "Hello" is a string literal and stored in read only memory. Since, ch stores the address of the first character in the string literal, so shouldn't the statement, cout<<ch; give the output " the address of a character " because, it is a pointer variable? Instead it prints the string literal itself. Moreover, if I write this, ch++; cout<<ch; It gives the output, " ello ". And similarly, it happens with more

Is it “bad practice” to use tab characters in string literals?

限于喜欢 提交于 2019-12-01 20:29:40
As a follow-up on Is it mandatory to escape tabulator characters in C and C++? (do note I'm not the author of said question). I've learned such code is considered "bad practice". The comments seem to be suggesting the same thing. However, for some reason the standard allows this trickery so somebody must either found no harm in it or has a use-case for it. Is not escaping tabulator characters widely accepted as "bad practice"? Using tabs in litterals instead of escaping them ( '\t' ,"\t") is a bad practice because : the reader is not immediately aware that there is a tab. Consequence: wrong

how to automatically escape the path

耗尽温柔 提交于 2019-12-01 17:47:53
问题 I have a path string like c:\user\test\test.jpg , how can I make it c:\\user\\test\\test.jpg ? 回答1: string s = s.Replace(@"\", @"\\"); 回答2: Try this: string path = @"c:\user\test\test.jpg"; 回答3: you would only require escaping if you are using string literal in the code. why would you require automatic escaping anyways. you can use @ before the literal that requires no escaping. 回答4: You can always try something like: System.Text.RegularExpressions.Regex.Unescape, of course that will do all

char four[4] = “four”; What are the correct semantics for this statement?

徘徊边缘 提交于 2019-12-01 16:37:20
问题 int main(void) { char four[4] = "four"; return 0; } When compiled as a C++ program, G++ reports xxx.cpp: In function int main(): xxx.cpp:3: error: initializer-string for array of chars is too long When compiled a a C program, GCC reports no error. It appears to me, that the assignment is correctly copying all 4 bytes into the variable, as I expected. So my question boils down to..... Is the observed behavior in C correct or am I touching an undefined behavior somewhere, or is it something

What happens with adjacent string literal concatenation when there is a modifier(L, u8, etc.)

随声附和 提交于 2019-12-01 16:16:50
It is valid in C and C++ to break a string literal because the preprocessor or the compiler will concatenate adjacent string literals. const char *zStr = "a" "b"; // valid What happens when string literals are prefixed with L (wide characters), u (UTF-16), U (UTF-32), u8 (UTF-8), and raw string literals ( R"foo(this is a "raw string literal" with double quotes)foo" )? For example, is the following allowed: const wchar_t *zStr = L"a" "b"; // valid? In C++0x your example is valid according to [lex.string]/p13: ... If one string literal has no encoding-prefix, it is treated as a string literal of