c-strings

Difference between char *str = “…” and char str[N] = “…”? [duplicate]

最后都变了- 提交于 2019-11-26 17:24:31
问题 Possible Duplicate: What is the difference between char s[] and char *s in C? Question about pointers and strings in C I'm reading about the strings in C and I'm confused. I can "declare" strings in two ways: char *str = "This is string"; char str2[20] = "This is string"; What is the difference between the two declarations? When would char str2[20] be preferred over char *str ? 回答1: char *str = "This is string"; Puts the string in the constant data section (also known as .rdata) of the

Returning 'c_str' from a function

一世执手 提交于 2019-11-26 17:05:01
问题 This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostringstream out; // ... rest of the function ... return out.str().c_str() } In my code I am doing this: const char *d = GetHandStateBrief(&post); std::cout<< d << std::endl; Now, at first d contained garbage. I then realized that the C string I am getting from the function is destroyed when the function returns because std::ostringstream is allocated on the stack. So I

Is sprintf(buffer, “%s […]”, buffer, […]) safe?

試著忘記壹切 提交于 2019-11-26 16:24:47
问题 I saw use of this pattern to concatenate onto a string in some code I was working on: sprintf(buffer, "%s <input type='file' name='%s' />\r\n", buffer, id); sprintf(buffer, "%s</td>", buffer); and I'm fairly certain it's not safe C. You'll notice that buffer is both the output and the first input. Apart from the obvious possibility of a buffer overflow , I believe there is no guarantee that buffer doesn't get changed between the start and the end of the function (i.e., there is no guarantee

Why gets() is deprecated? [duplicate]

孤者浪人 提交于 2019-11-26 14:58:46
问题 This question already has an answer here: Why is the gets function so dangerous that it should not be used? 11 answers While using gets() in my code, the compiler shouts warning: the 'gets' function is dangerous and should not be used.` and warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] Any specific reasons? 回答1: Can someone explains why the compiler shows like that…? Yes, because, the gets() function is dangerous, as it suffers from buffer

Convert String^ in c# to CString in c++/CLI

孤街浪徒 提交于 2019-11-26 14:52:35
问题 I need a help on one question where I stuck while coding my app in MFC . I am using CLR i.e Common Language Runtime in my application to integrate c# APIs. but now I stuck on converting System::String^ to CString . I am not able to do that. I am using Following code. String^ csPass = gcnew String(strPassword.GetBuffer()); array<Byte>^ Value = Encoding::UTF8->GetBytes(csPass); for (int i = 0; i < Value->Length; i++ ) { csPass += String::Format( "{0:X2}", Value[ i ] ); } now I want to convert

String termination - char c=0 vs char c=&#39;\0&#39;

二次信任 提交于 2019-11-26 13:02:37
问题 When terminating a string, it seems to me that logically char c=0 is equivalent to char c=\'\\0\' , since the \"null\" (ASCII 0) byte is 0 , but usually people tend to do \'\\0\' instead. Is this purely out of preference or should it be a better \"practice\"? What is the preferred choice? EDIT: K&R says : \"The character constant \'\\0\' represents the character with value zero, the null character. \'\\0\' is often written instead of 0 to emphasize the character nature of some expression, but

C Strings Comparison with Equal Sign

匆匆过客 提交于 2019-11-26 09:59:37
问题 I have this code: char *name = \"George\" if(name == \"George\") printf(\"It\'s George\") I thought that c strings could not be compared with == sign and I have to use strcmp . For unknown reason when I compile with gcc (version 4.7.3) this code works. I though that this was wrong because it is like comparing pointers so I searched in google and many people say that it\'s wrong and comparing with == can\'t be done. So why this comparing method works ? 回答1: I thought that c strings could not

C - split string into an array of strings

一个人想着一个人 提交于 2019-11-26 06:32:47
问题 I\'m not completely sure how to do this in C: char* curToken = strtok(string, \";\"); //curToken = \"ls -l\" we will say //I need a array of strings containing \"ls\", \"-l\", and NULL for execvp() How would I go about doing this? 回答1: Since you've already looked into strtok just continue down the same path and split your string using space ( ' ' ) as a delimiter, then use something as realloc to increase the size of the array containing the elements to be passed to execvp . See the below

Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”?

老子叫甜甜 提交于 2019-11-25 22:10:29
问题 The following code receives seg fault on line 2: char *str = \"string\"; str[0] = \'z\'; // could be also written as *str = \'z\' printf(\"%s\\n\", str); While this works perfectly well: char str[] = \"string\"; str[0] = \'z\'; printf(\"%s\\n\", str); Tested with MSVC and GCC. 回答1: See the C FAQ, Question 1.32 Q : What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i] . A : A