c-strings

Is it possible to print out only a certain section of a C-string, without making a separate substring?

微笑、不失礼 提交于 2019-11-27 11:52:39
Say I have the following: char* string = "Hello, how are you?"; Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printf that would allow for this? Is it possible to print out only the last 5 bytes of this string? Yes, just pass a pointer to the fifth-to-the-last character. You can determine this by string + strlen(string) - 5 . What about the first 5 bytes only? Use a precision specifier: %.5s #include <stdio.h> #include <string.h> char* string = "Hello, how are you?"; int main() { /* print at most the first five

Why gets() is deprecated? [duplicate]

梦想与她 提交于 2019-11-27 09:52:27
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? Can someone explains why the compiler shows like that…? Yes, because, the gets() function is dangerous, as it suffers from buffer overflow issue. Anyone should refrain from using that. Also, regarding the warning with -Wdeprecated

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

血红的双手。 提交于 2019-11-27 09:41:37
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 csPass to CString . Can any one help me on this. Thank you in advance. Ilya Tereschuk Consider reading

Access violation writing location when working with pointers to char

那年仲夏 提交于 2019-11-27 07:52:12
问题 I am writing a very simple program that removes duplicate chars from a string. I ran it visual studio and got the error: Unhandled exception at 0x00d110d9 in inteviews.exe: 0xC0000005: Access violation writing location 0x00d27830. I really don't see what the problem is. current cell gets the value of the next cell. void remove(char *str, char a) { while (*str != '\0') { if (*(str+1) == a) { remove(str + 1, a); } *str = *(str +1 );//HERE I GET THE ERROR ++str; } } int _tmain(int argc, _TCHAR*

Why Doesn't string::data() Provide a Mutable char*?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 07:35:52
问题 In c++11 array , string , and vector all got the data method which: Returns pointer to the underlying array serving as element storage. The pointer is such that range [ data() ; data() + size() ) is always a valid range, even if the container is empty. [Source] This method is provided in a mutable and const version for all applicable containers, for example: T* vector<T>::data(); const T* vector<T>::data() const; All applicable containers, that is, except string which only provides the const

String termination - char c=0 vs char c='\\0'

对着背影说爱祢 提交于 2019-11-27 07:04:06
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 the numeric value is just 0 . http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart Binary Oct Dec Hex Abbr

How to extract a substring from a string in C?

微笑、不失礼 提交于 2019-11-27 06:52:13
问题 I tried using strncmp but it only works if I give it a specific number of bytes I want to extract. char line[256] = This "is" an example. //I want to extract "is" char line[256] = This is "also" an example. // I want to extract "also" char line[256] = This is the final "example". // I want to extract "example" char substring[256] How would I extract all the elements in between the ""? and put it in the variable substring? 回答1: Note: I edited this answer after I realized that as written the

What is the reason for not being able to deduce array size from initializer-string in member variable?

扶醉桌前 提交于 2019-11-26 21:47:05
问题 Consider the code: struct Foo { const char str[] = "test"; }; int main() { Foo foo; } It fails to compile with both g++ and clang++, spitting out essentially error: array bound cannot be deduced from an in-class initializer I understand that this is what the standard probably says, but is there any particular good reason why? Since we have a string literal it seems that the compiler should be able to deduce the size without any problem, similarly to the case when you simply declare an out-of

C - split string into an array of strings

巧了我就是萌 提交于 2019-11-26 18:41:57
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? 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 example, but keep in mind that strtok will modify the string passed to it. If you don't want this to happen you are

Is it possible to print out only a certain section of a C-string, without making a separate substring?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:07:08
问题 Say I have the following: char* string = "Hello, how are you?"; Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printf that would allow for this? 回答1: Is it possible to print out only the last 5 bytes of this string? Yes, just pass a pointer to the fifth-to-the-last character. You can determine this by string + strlen(string) - 5 . What about the first 5 bytes only? Use a precision specifier: %.5s #include <stdio