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
62
votes
5 answers

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 with how it satisfies the string requirement given…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
55
votes
7 answers

Converting a C-style string to a C++ std::string

What is the best way to convert a C-style string to a C++ std::string? In the past I've done it using stringstreams. Is there a better way?
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
52
votes
10 answers

What is the difference between str==NULL and str[0]=='\0' in C?

I want to know the difference between str == NULL and str[0] == '\0': int convert_to_float(char *str, double *num) { if ((str == NULL) || (str[0] == '\0')) return(-1); *num = strtod(str, (char **)NULL); return(0); } I'm using…
john
  • 1,323
  • 2
  • 19
  • 31
43
votes
4 answers

What does _T stands for in a CString

What does the "T" represents in a string. For example _T("Hello").I have seen this in projects where unicode support is needed.What it actually tells the processor
CodeRider
  • 1,750
  • 6
  • 18
  • 34
30
votes
2 answers

Working with C strings in Swift, or: How to convert UnsafePointer to CString

While playing with Standard C Library functions in Swift, I came across problems when passing C strings around. As a simple example (just to demonstrate the problem), the Standard C Library function char * strdup(const char *s1); is exposed to…
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
30
votes
8 answers

Initializing a Char*[]

Question is in the title, how do I initialize a char*[] and give values to it in C++, thank you.
DogDog
  • 4,820
  • 12
  • 44
  • 66
29
votes
8 answers

How to remove first character from C-string?

Can anyone please help me? I need to remove the first character from a char * in C. For example, char * contents contains a '\n' character as the first character in the array. I need to detect and eliminate this character, modifying the original…
Ash
  • 24,276
  • 34
  • 107
  • 152
25
votes
4 answers

MFC: std::string vs CString?

Using C++ with MFC. Coming from a C# background I typically just use string for all, well, strings. I use them for class members, method parameters, and method return values. Now in C++ I've got std::string, CString, char *, LPCTSTR, and more. …
User
  • 62,498
  • 72
  • 186
  • 247
22
votes
3 answers

g++ 4.6 issue no file as required by the header cstring

There is no file called bits/c++config.h in the c++ include directory which is required by the cstring header file. But when I include the the header cstring and compile with g++, it does not give me error. The problem occurred when I tried to…
A. K.
  • 34,395
  • 15
  • 52
  • 89
22
votes
2 answers

Matplotlib, alternatives to savefig() to improve performance when saving into a CString object?

I am trying to speed up the process of saving my charts to images. Right now I am creating a cString Object where I save the chart to by using savefig; but I would really, really appreciate any help to improve this method of saving the image. I have…
relima
  • 3,462
  • 5
  • 34
  • 53
22
votes
7 answers

Help with \0 terminated strings in C#

I'm using a low level native API where I send an unsafe byte buffer pointer to get a c-string value. So it gives me // using byte[255] c_str string s = new string(Encoding.ASCII.GetChars(c_str)); // now s ==…
y2k
  • 65,388
  • 27
  • 61
  • 86
17
votes
4 answers

CString to char*

We are using the CString class throughout most of our code. However sometimes we need to convert to a char *. at the moment we have been doing this using variable.GetBuffer(0) and this seems to work ( this mainly happens when passing the Csting into…
Mark Lakewood
  • 1,980
  • 4
  • 22
  • 44
17
votes
10 answers

How can I hash a string to an int using c++?

I have to write my own hash function. If I wanted to just make the simple hash function that maps each letter in the string to a numerical value (i.e. a=1, b=2, c=3, ...), is there a way I can perform this hash on a string without having to first…
zebraman
  • 2,424
  • 5
  • 20
  • 21
17
votes
3 answers

UTF-8, CString and CFile? (C++, MFC)

I'm currently working on a MFC program that specifically has to work with UTF-8. At some point, I have to write UTF-8 data into a file; to do that, I'm using CFiles and CStrings. When I get to write utf-8 (russian characters, to be more precise)…
SeargX
  • 117
  • 1
  • 2
  • 10
15
votes
2 answers

CString to LPCTSTR conversion

I have a CString variable that i a need to convert to LPCTSTR(const char*) .I need this conversion so that i can use it as an argument in a function . The CString look like : CString sqlTemp = _T("INSERT INTO "+ sw1 +" (filename, "+ sw2 +") VALUE…
Ionut Daniel
  • 312
  • 2
  • 9
  • 20
1
2 3
32 33