Questions tagged [cstring]

Refers to 0-terminated strings as popularized by C, as well as the header-files `string.h` and `cstring`.

A C string is a sequence of non-zero bytes terminated by a NUL byte (0x00), usually used to store ASCII or UTF-8 text.

C strings are defined as char arrays in C and C++, where char is an 8-bit type. C strings can be referred to using a char pointer.

Extensions to the basic C string (e.g. to 16-bit wchar_t "wide strings") also exist in various programming environments.

In C++, the standard std::string class interoperates with C strings via the .c_str() method, though beware that std::strings can contain embedded 0-bytes.

488 questions
7
votes
3 answers

Which wide-character string structure do I use? CString vs wstring

I have an MFC application in C++ that uses std::string and std::wstring, and frequently casts from one to the other, and a whole lot of other nonsense. I need to standardize everything to a single format, so I was wondering if I should go with…
Vanwaril
  • 7,380
  • 6
  • 33
  • 47
7
votes
3 answers

How should I allocate memory for c-string char array?

So in attempting to learn how to use C-Strings in C++, I'm running into issues with memory allocation. The idea here is that a new string is created of the format (s1 + sep + s2) The text I'm using provided the header, so I can't change that, but…
7
votes
3 answers

C string map key

Is there any issue with using a C string as a map key? std::map imap; The order of the elements in the map doesn't matter, so it's okay if they are ordered using std::less. I'm using Visual Studio and according to…
user974967
  • 2,928
  • 10
  • 28
  • 45
7
votes
1 answer

Segmentation Fault: "...no such file or directory"

I'm getting weird seg fault that seems to be coming from somewhere not in my program...not explicitly anyway. I'm calling "strcmp" on two array's... Both arrays are stored in the same type of structs. I'm getting at one with dot notation and one…
MCP
  • 4,436
  • 12
  • 44
  • 53
6
votes
3 answers

(How) can I use the Boost String Algorithms Library with c strings (char pointers)?

Is it possible to somehow adapt a c-style string/buffer (char* or wchar_t*) to work with the Boost String Algorithms Library? That is, for example, it's trimalgorithm has the following declaration: template void trim(SequenceT…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
6
votes
7 answers

Environment Variables are in a char* how to get it to a std::string

I am retrieving the environment variables in win32 using GetEnvironmentStrings(). It returns a char*. I want to search this string(char pointer) for a specific environmental variable (yes I know I can use GetEnvironmentVariable() but I am doing it…
Mack
  • 63
  • 1
  • 3
6
votes
20 answers

How would you improve this algorithm? (c string reversal)

Working through some programming interview challenges I found online, I had to write an algorithm to reverse a const char * and return a pointer to a new char *. I think I have it, but to make it work properly I had to do some wonky stuff -…
JDS
6
votes
2 answers

Calling a C Function with C-style Strings

I'm struggling to call mktemp in D: import core.sys.posix.stdlib; import std.string: toStringz; auto name = "alpha"; auto tmp = mktemp(name.toStringz); but I can't figure out how to use it so DMD complains: /home/per/Work/justd/fs.d(1042): Error:…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
6
votes
3 answers

CString maximum length

What is the maximum length in characters a CString object can hold?
LLucasAlday
  • 2,349
  • 11
  • 34
  • 41
5
votes
10 answers

How could this C fragment be written more safely?

I have the following C code fragment and have to identify the error and suggest a way of writing it more safely: char somestring[] = "Send money!\n"; char *copy; copy = (char *) malloc(strlen(somestring)); strcpy(copy,…
Adam Taylor
  • 7,534
  • 8
  • 44
  • 54
5
votes
1 answer

How do I swap an MFC CString?

OK, so I'm all sold on the copy-and-swap idiom and I think I mostly know how to implement it. However, or codebase uses MFC's CString class as string and this ain't gonna change. Since swap must (should???) be nothrow, I cannot…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
5
votes
1 answer

How do I convert an ATL/MFC CString to a QString?

Given the encoding of the project is probably Unicode (but not for sure) what is the best way of converting ATL::CString to QString? What I have thought of is this: CString c(_T("SOME_TEXT")); //... std::basic_string
Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
5
votes
3 answers

How to write INT64 to CString

I am coding in c++ windows. INT64 dirID = -1; CString querySQLStr = _T(""); querySQLStr.Format(L"select * from ImageInfo where FolderPath=%64d;", dirID); querySQLStr always like this: select * from ImageInfo where FolderPath= …
sxingfeng
  • 971
  • 4
  • 15
  • 32
5
votes
3 answers

Convert String to UnsafeMutablePointer in Swift

I'm working with a third party c API I'm trying to call one of the functions with a simple string. Something like this: some_c_func("aString"); I get a build error: Type 'UnsafeMutablePointer' does not conform to protocol…
brodney
  • 1,176
  • 2
  • 14
  • 29
5
votes
1 answer

Unable to convert the CStringW to CStringA

I am working on one project where I have stucked on one problem of converting CStringW to CStringA for multibyte string like Japanese Language. I am loading the string from string resources using LoadString() Method. I have tried following code but…
A B
  • 1,461
  • 2
  • 19
  • 54
1 2
3
32 33