string-literals

Is it possible to initialise a character array with a conditionally selected string literal?

為{幸葍}努か 提交于 2019-12-03 05:22:41
I know it's perfectly possible to initialise a char array with a string literal: char arr[] = "foo"; C++11 8.5.2/1 says so: A char array (whether plain char , signed char , or unsigned char ), char16_t array, char32_t array, or wchar_t array can be initialized by a narrow character literal, char16_t string literal, char32_t string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces. Successive characters of the value of the string literal initialize the elements of the array. ... However, can you do the same with two string literals in

Restricting string literals to Text only

a 夏天 提交于 2019-12-02 21:46:31
I'm aware that the OverloadedStrings language pragma wraps an implicit fromString around all string literals. What I'd like to do is not actually overload strings, but merely change their meaning so that they are always turned into Text , and therefore, using a string literal as a list of characters should result in a type error. It appears to be impossible to import the IsString class without also importing the String instance for that class. Does ghc provide some way for me to restrict string literals to Text only? It's a little bit of overkill, but one solution is to combine

String Literal Differences Between C and C++

江枫思渺然 提交于 2019-12-02 20:39:50
As far as I can tell, before C++11, string literals were handled in almost exactly the same way between C and C++. Now, I acknowledge that there are differences between C and C++ in the handling of wide string literals. The only differences that I have been able to find are in the initialization of an array by string literal. char str[3] = "abc"; /* OK in C but not in C++ */ char str[4] = "abc"; /* OK in C and in C++. Terminating zero at str[3] */ And a technical difference that only matters in C++. In C++ "abc" is const char [4] while in C it is char [4] . However, C++ has a special rule that

Why do (only) some compilers use the same address for identical string literals?

旧时模样 提交于 2019-12-02 15:41:44
https://godbolt.org/z/cyBiWY I can see two 'some' literals in assembler code generated by MSVC, but only one with clang and gcc. This leads to totally different results of code execution. static const char *A = "some"; static const char *B = "some"; void f() { if (A == B) { throw "Hello, string merging!"; } } Can anyone explain the difference and similarities between those compilation outputs? Why does clang/gcc optimize something even when no optimizations are requested? Is this some kind of undefined behaviour? I also notice that if I change the declarations to those shown below, clang/gcc

When we can or cannot modify String Literals [duplicate]

拈花ヽ惹草 提交于 2019-12-02 13:34:20
This question already has an answer here: Segmentation fault when modifying a string [duplicate] 2 answers #include<stdio.h> int main () { char *s="FIGHT" ; printf("\n Whole string is %s ", s ); // Printing FIGHT -- this is fine s[0]='L' ; printf ("\n Now whole string is %s", s ); // Printing LIGHT -- My Question is how string literal constant is getting modified when it is being stored in read only memory . } Above Code is working fine on my system. TL;DR -- Never. Any attempt to modify a string literal invokes undefined behavior . To quote the C11 standard, chapter §6.4.5, String literals [.

What does 'R' mean in the context of string literals?

久未见 提交于 2019-12-02 10:43:06
问题 This code basically talks to a AMPS server and tries to publish a topic. What is meaning of R in the second parameter of the publish( )? #include <ampsplusplus.hpp> #include <iostream> int main(void) { const char* uri = "tcp://127.0.0.1:9007/amps/json"; // Construct a client with the name "examplePublisher". AMPS::Client ampsClient("examplePublisher"); try { // connect to the server and log on ampsClient.connect(uri); ampsClient.logon(); // publish a JSON message ampsClient.publish("messages"

What does the \newline escape sequence mean in python?

痞子三分冷 提交于 2019-12-02 08:56:15
问题 I found the sequence \newline in a list of escape sequences in the python documentation. I wonder how it is used and for what. At least in my interpreter it seems this is just interpreted as '\n' + 'ewline' : >>> print('\newline') ewline 回答1: It refers to the actual newline character - the one with character code "16" (0x10) - not the text sequence "newline". So, an example is like: print("a\ b") Here, the backslash is succeeded by the newline, inside a string, and what is printed is just "ab

Distinguish a string literal from a string C#

我与影子孤独终老i 提交于 2019-12-02 07:31:24
I want to do something like IsItAStringLiteral("yes") var v = "no"; IsItAStringLiteral(v) With the obvious return value. Is it possible? You can use the string.IsInterned method to determine whether a given string is in the .NET intern pool. All string literals are automatically added to the intern pool before the application begins running. Of course, that won't help you with your exact question. Variable 'v' will reference a string literal, so it too will appear in the intern pool. Why do you need such functionality? No. You won't be able to tell whether a string is a literal or not. The

PHP dynamically reference variable in string literal

我的梦境 提交于 2019-12-02 04:52:54
问题 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}"; } 回答1: This

What does 'R' mean in the context of string literals?

我怕爱的太早我们不能终老 提交于 2019-12-02 03:06:02
This code basically talks to a AMPS server and tries to publish a topic. What is meaning of R in the second parameter of the publish( )? #include <ampsplusplus.hpp> #include <iostream> int main(void) { const char* uri = "tcp://127.0.0.1:9007/amps/json"; // Construct a client with the name "examplePublisher". AMPS::Client ampsClient("examplePublisher"); try { // connect to the server and log on ampsClient.connect(uri); ampsClient.logon(); // publish a JSON message ampsClient.publish("messages", R"({ "message" : "Hello, World!" ,)" R"(client" : 1 })"); } catch (const AMPS::AMPSException& e) {