string-literals

auto with string literals

老子叫甜甜 提交于 2019-12-18 03:04:48
问题 #include <iostream> #include <typeinfo> int main() { const char a[] = "hello world"; const char * p = "hello world"; auto x = "hello world"; if (typeid(x) == typeid(a)) std::cout << "It's an array!\n"; else if (typeid(x) == typeid(p)) std::cout << "It's a pointer!\n"; // this is printed else std::cout << "It's Superman!\n"; } Why is x deduced to be a pointer when string literals are actually arrays? A narrow string literal has type "array of n const char " [2.14.5 String Literals [lex.string]

In Rust, can you own a string literal?

佐手、 提交于 2019-12-17 21:00:12
问题 According to The Rust book: Each value in Rust has a variable that’s called its owner. There can be only one owner at a time. When the owner goes out of scope, the value will be dropped. According to rust-lang.org: Static items do not call drop at the end of the program. After reading this SO post, and given the code below, I understand that foo is a value whose variable y , equivalent to &y since "string literals are string slices", is called its owner . Is that correct? Or do static items

g++ treats returned string literal as const char pointer not const char array

核能气质少年 提交于 2019-12-17 18:54:21
问题 I'm seeing some odd behaviour when returning a string literal from a function that should perform an implicit conversion with g++ (version 4.7.3). Can anyone explain why the following code: #include <stdio.h> class Test { public: template <unsigned int N> Test(const char (&foo)[N]) { printf("Template const char array constructor\n"); } Test(char* foo) { printf("char* constructor\n"); } }; Test fn() { return "foo"; } int main() { Test t("bar"); Test u = fn(); return 0; } produces the result:

String Literal address across translation units [duplicate]

梦想与她 提交于 2019-12-17 02:31:55
问题 This question already has answers here : Addresses of two char pointers to different string literals are same (10 answers) Closed 2 years ago . I'd like to ask if is it portable to rely on string literal address across translation units? I.e: A given file foo.c has a reference to a string literal "I'm a literal!" , is it correct and portable to rely that in other given file, bar.c in instance, that the same string literal "I'm a literal!" will have the same memory address ? Considering that

“life-time” of string literal in C

瘦欲@ 提交于 2019-12-16 22:12:10
问题 Wouldn't the pointer returned by the following function inaccessible? char *foo( int rc ) { switch (rc) { case 1: return("one"); case 2: return("two"); default: return("whatever"); } } So the lifetime of a local variable in C/C++ is practically only within the function, right? Which means, after char* foo(int) terminates, the pointer it returns no longer means anything? I'm a bit confused about lifetime of local var. Could anyone give me a good clarification? 回答1: Yes, lifetime of an local

Inconsistency between std::string and string literals

对着背影说爱祢 提交于 2019-12-14 00:14:54
问题 I have discovered a disturbing inconsistency between std::string and string literals in C++0x: #include <iostream> #include <string> int main() { int i = 0; for (auto e : "hello") ++i; std::cout << "Number of elements: " << i << '\n'; i = 0; for (auto e : std::string("hello")) ++i; std::cout << "Number of elements: " << i << '\n'; return 0; } The output is: Number of elements: 6 Number of elements: 5 I understand the mechanics of why this is happening: the string literal is really an array of

Can a string literal and a non-string non-compound literal be modified? [duplicate]

限于喜欢 提交于 2019-12-13 09:49:30
问题 This question already has answers here : Why are compound literals in C modifiable (2 answers) Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”? (17 answers) Closed last year . String literals are lvalues, which leaves the door open to modify string literals. From C in a Nutshell: In C source code, a literal is a token that denotes a fixed value , which may be an integer, a floating-point number, a character, or a string. A literal’s

Issue comparing NSString literal and constant

空扰寡人 提交于 2019-12-13 07:01:05
问题 I have an NSString , firstWord , that has the value of "First". It was defined like this: NSString *firstWord = [[NSString alloc] init]; firstWord = [textView text]; However, when I try to check its value, like so: if (firstWord == @"First"){} The comparison does not return true, or YES I guess in Objective-C. I know that it does have the same value as a string. So, from this I am pretty sure that the issue is that I am comparing pointers, which are not the same here, even though the strings

how to access sys.argv (or any string variable) in raw mode?

烂漫一生 提交于 2019-12-13 02:09:07
问题 I'm having difficulties parsing filepaths sent as arguments: If I type: os.path.normpath('D:\Data2\090925') I get 'D:\\Data2\x0090925' Obviously the \0 in the folder name is upsetting the formatting. I can correct it with the following: os.path.normpath(r'D:\Data2\090925') which gives 'D:\\Data2\\090925' My problem is, how do I achieve the same result with sys.argv? Namely: os.path.normpath(sys.argv[1]) I can't find a way for feeding sys.argv in a raw mode into os.path.normpath() to avoid

Is storage for the same content string literals guaranteed to be the same?

◇◆丶佛笑我妖孽 提交于 2019-12-12 07:17:14
问题 Is the code below safe? It might be tempting to write code akin to this: #include <map> const std::map<const char*, int> m = { {"text1", 1}, {"text2", 2} }; int main () { volatile const auto a = m.at("text1"); return 0; } The map is intended to be used with string literals only. I think it's perfectly legal and seems to be working, however I never saw a guarantee that the pointer for the literal used in two different places to be the same. I couldn't manage to make compiler generate two