string-literals

Why can a string literal be implicitly converted to char* only in certain case? [duplicate]

霸气de小男生 提交于 2019-12-21 07:25:31
问题 This question already has answers here : Why is passing a string literal into a char* argument only sometimes a compiler error? (6 answers) Closed 6 years ago . void f(char* p) {} int main() { f("Hello"); // OK auto p = "Hello"; f(p); // error C2664: 'void f(char *)' : cannot convert parameter 1 // from 'const char *' to 'char *' } The code was compiled with VC++ Nov 2012 CTP. §2.14.15 String Literals, Section 7 A narrow string literal has type “array of n const char”, where n is the size of

C - char array and char pointer

℡╲_俬逩灬. 提交于 2019-12-21 05:25:10
问题 Why I can't define an array char **pp={ "123", "456", "789" }; But I can define it as a char*[] ,and send it to a function that will accept it as a char ** char *pp[]={ "123", "456", "789" }; fun(pp); void fun(char **pointerToPointer) { //++(**pointerToPointer);//error printf("%s", *pointerToPointer); } //output::"123" And why I can't increment ++(**pointerToPointer); 回答1: To answer the first question, the principles might be clearer if we use a single depth of pointer. This code is illegal

Distinguish a string literal from a string C#

大城市里の小女人 提交于 2019-12-20 06:15:22
问题 I want to do something like IsItAStringLiteral("yes") var v = "no"; IsItAStringLiteral(v) With the obvious return value. Is it possible? 回答1: 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

strtok Unhandled exception;Access violation writing location

我是研究僧i 提交于 2019-12-20 05:52:18
问题 #include <stdio.h> #include <time.h> #include <string.h> char *matrix[10][10]; int main(void) { int i; char *list[4]; char *words[20] = { " c a t ", " c a r ", " b e a r ", " s h i p ", " m o u s e ", " b e a t l e ", " c o a t ", " n e s t ", " i c e ", " s u g a r ", " b a c o n ", " f r o w n ", " s m i l e ", " d e a d ", " f e a t h e r ", " g o a t ", " h e n "," j e l l y "," k o a l a "," l i p s " }; int length; int num; int k; int m; char otherString=0; char *c; int j; int s; int r;

Why it is possible to assign string to character pointer in C but not an integer value to an integer pointer

牧云@^-^@ 提交于 2019-12-19 09:23:27
问题 why in the below code int *p = 22 will give compile time error and ptr will print the value successfully . int main() { /*taking a character pointer and assigning a string to it*/ char *ptr = "Stackoverflow" ; //correct /*taking a int pointer and assigning a string to it*/ int *p = 22 ; //incorrect printf("%s",ptr); // correct and print printf("%d",p); //incorrect and give compile time error. return 0; } 回答1: If you have a character array as for example char s[] = "Stackoverflow"; then the

Raw string literals and file codification

邮差的信 提交于 2019-12-19 09:08:42
问题 C++11 introduced the raw string literals which can be pretty useful to represent quoted strings, literals with lots of special symbols like windows file paths, regex expressions etc... std::string path = R"(C:\teamwork\new_project\project1)"; // no tab nor newline! std::string quoted = R"("quoted string")"; std::string expression = R"([\w]+[ ]+)"; This raw string literals can also be combined with encoding prefixes ( u8 , u , U , or L ), but, when no encoding prefix is specified, does the

Implementation of string literal concatenation in C and C++

混江龙づ霸主 提交于 2019-12-19 06:42:17
问题 AFAIK, this question applies equally to C and C++ Step 6 of the "translation phases" specified in the C standard (5.1.1.2 in the draft C99 standard) states that adjacent string literals have to be concatenated into a single literal. I.e. printf("helloworld.c" ": %d: Hello " "world\n", 10); Is equivalent (syntactically) to: printf("helloworld.c: %d: Hello world\n", 10); However, the standard doesn't seem to specify which part of the compiler has to handle this - should it be the preprocessor (

How does file encoding affect C++11 string literals?

倾然丶 夕夏残阳落幕 提交于 2019-12-19 05:19:12
问题 You can write UTF-8/16/32 string literals in C++11 by prefixing the string literal with u8 / u / U respectively. How must the compiler interpret a UTF-8 file that has non-ASCII characters inside of these new types of string literals? I understand the standard does not specify file encodings, and that fact alone would make the interpretation of non-ASCII characters inside source code completely undefined behavior, making the feature just a tad less useful. I understand you can still escape

Are hard-coded STRINGS ever acceptable?

自闭症网瘾萝莉.ら 提交于 2019-12-19 04:21:30
问题 Similar to Is hard-coding literals ever acceptable?, but I'm specifically thinking of "magic strings" here. On a large project, we have a table of configuration options like these: Name Value ---- ----- FOO_ENABLED Y BAR_ENABLED N ... (Hundreds of them). The common practice is to call a generic function to test an option like this: if (config_options.value('FOO_ENABLED') == 'Y') ... (Of course, this same option may need to be checked in many places in the system code.) When adding a new

Adding a string to the verbatim string literal

烈酒焚心 提交于 2019-12-19 03:09:42
问题 I have a path that is named defaultPath I want to add it into this verbatim string literal but can quite get the quotes around it. @"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\Data""" I was trying to add +defaultPath to replace Data. So lets say I have a folder name Data.Apple I want the output to be "C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\Data.Apple" But when I have been doing it for the past half hour I have been getting "C:\Mavro