string-literals

How to use Macro argument as string literal?

Deadly 提交于 2019-11-27 12:32:17
I am trying to figure out how to write a macro that will pass both a string literal representation of a variable name along with the variable itself into a function. For example given the following function. void do_something(string name, int val) { cout << name << ": " << val << endl; } I would want to write a macro so I can do this: int my_val = 5; CALL_DO_SOMETHING(my_val); Which would print out: my_val: 5 I tried doing the following: #define CALL_DO_SOMETHING(VAR) do_something("VAR", VAR); However, as you might guess, the VAR inside the quotes doesn't get replaced, but is just passed as

String literals: pointer vs. char array

橙三吉。 提交于 2019-11-27 11:59:10
In this statement: char *a = "string1" What exactly is string literal? Is it string1 ? Because this thread What is the type of string literals in C and C++? says something different. Up to my knowledge int main() { char *a = "string1"; //is a string- literals allocated memory in read-only section. char b[] = "string2"; //is a array char where memory will be allocated in stack. a[0] = 'X'; //Not allowed. It is an undefined Behaviour. For me, it Seg Faults. b[0] = 'Y'; //Valid. return 0; } Please add some details other than above mentioned points. Thanks. Debug Output Showing error in a[0] = 'Y'

How to make Jade stop HTML encoding element attributes, and produce a literal string value?

ぐ巨炮叔叔 提交于 2019-11-27 11:57:47
问题 UPDATE Jade v0.24.0 fixes this with a != syntax for attributes. option(value!='<%= id %>') I'm trying to build an <option> with jade, where the value of the option is an UnderscoreJS template marker: <%= id %> but I can't get it to work because jade is converting my marker text to <= id > . Here's my Jade markup: script(id="my-template", type="text/template") select(id="type") <% _.each(deviceTypes, function(type){ %> option(value='<%= type.id %>') <%= type.name %> <% }) %> I expect it to

C -> sizeof string is always 8

南楼画角 提交于 2019-11-27 09:23:40
#include "usefunc.h" //don't worry about this -> lib I wrote int main() { int i; string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array given[0] = "a"; printf("Please enter words separated by RETs...\n"); for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++) { given[i] = GetLine(); /* if (sizeof(given[i]) > sizeof(longest)) { longest = given[i]; } */ printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!! } printf("%s", longest); } Why does it always return 8??? There is no string data type in C. Is this C++? Or is string a typedef? Assuming

Comparing character arrays and string literals in C++

六月ゝ 毕业季﹏ 提交于 2019-11-27 07:54:07
问题 I have a character array and I'm trying to figure out if it matches a string literal, for example: char value[] = "yes"; if(value == "yes") { // code block } else { // code block } This resulted in the following error: comparison with string literal results in unspecified behavior. I also tried something like: char value[] = "yes"; if(strcmp(value, "yes")) { // code block } else { // code block } This didn't yield any compiler errors but it is not behaving as expected. 回答1: std::strcmp

Isn't there a syntax error? Should printf(“one” “, two and ” “%s.\n”, “three” ); be valid code?

∥☆過路亽.° 提交于 2019-11-27 07:17:33
问题 Take a look at this code: #include <stdio.h> #define _ONE "one" #define _TWO_AND ", two and " int main() { const char THREE[6] = "three" ; printf(_ONE _TWO_AND "%s.\n", THREE ); return 0; } The printf is effectively: printf("one" ", two and " "%s.\n", "three" ); and the output is: one, two and three. gcc gives neither error nor warning messages after compiling this code. Is the gcc compiler supposed work in that way, or is it a bug? 回答1: This is standard behavior, adjacent string literals are

Unary plus (+) against literal string

岁酱吖の 提交于 2019-11-27 06:41:09
问题 Today I wrote an expression: "<" + message_id + "@" + + ">" ^ | \____ see that extra '+' here! and got surprised that it actually compiled. (PS message_id is a QString , it would also work with an std::string ) I often do things like that, leave out a variable as I'm working and I expect the compiler to tell me where I'm still missing entries. The final would look something like this: "<" + message_id + "@" + network_domain + ">" Now I'd like to know why the + unary operator is valid against

Why allow concatenation of string literals?

那年仲夏 提交于 2019-11-27 05:34: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 this a "feature" in a modern language like python? Have you ever used string literal concatenation in production code?

String literals not allowed as non type template parameters

删除回忆录丶 提交于 2019-11-27 03:39:04
The following quote is from C++ Templates by Addison Wesley . Could someone please help me understand in plain English/layman's terms its gist? Because string literals are objects with internal linkage (two string literals with the same value but in different modules are different objects), you can't use them as template arguments either: GManNickG Your compiler ultimately operates on things called translation units , informally called source files . Within these translation units, you identify different entities: objects, functions, etc. The linkers job is to connect these units together, and

C# two double quotes

北城以北 提交于 2019-11-27 03:20:02
问题 I want to print two double quotes in C# as the output. How to do this? I mean the output should be: "" Hello World "" 回答1: Console.WriteLine("\"\" Hello world \"\""); or Console.WriteLine(@""""" Hello world """""); 回答2: If you want to put double quotes in a string you need to escape them with a \ eg: string foo = "here is a \"quote\" character"; If you want to literally output "" Hello World "" then you'd need: string helloWorld = "\"\" Hello World \"\""; output(helloWorld); (where output is