All Questions

204
votes
16answers
50431 views

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

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s...
74
votes
6answers
19231 views

What is std::string::c_str() lifetime?

In one of my programs, I have to interface with some legacy code that works with const char*. Lets say I have a structure which looks like: struct Foo { const char* server; const char* name; }; ...
44
votes
5answers
38708 views

How is std::string implemented?

I am curious to know how std::string is implemented and how does it differ from c string?If the standard does not specify any implementation then any implementation with explanation would be great wit...
31
votes
2answers
76295 views

C - split string into an array of strings

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 wou...
6
votes
5answers
732 views

C Strings Comparison with Equal Sign

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 ...
63
votes
10answers
135453 views

How to convert CString and ::std::string ::std::wstring to each other?

CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CString as key, so I want to convert CString into std::string. ...
43
votes
2answers
54738 views

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

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 print...
21
votes
6answers
17160 views

Why is strdup considered to be evil

I've seen some posters stating that strdup is evil. Is there a consensus on this? I've used it without any guilty feelings and can see no reason why it is worse than using malloc/memcpy. The only t...
8
votes
2answers
4733 views

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

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...
7
votes
4answers
9327 views

Proper way to copy C-strings

Is there an easy way to copy C-strings? I have const char *stringA, and I want char *stringB to take the value (note that stringB is not const). I tried stringB=(char*) stringA, but that makes string...

Previous Next