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
3
votes
4 answers

Stored value disappears when setting a struct pointer to null in C++

I'm writing a C++ application to do a word search across a large database of song lyrics. to start, I'm taking each word and putting it into a Word struct that looks like this: struct Word{ char* clean; int size; int position; SongId…
Sarah
  • 516
  • 10
  • 35
3
votes
3 answers

C strcat garbage characters

I have a function in C where i am trying to get strings from two different locations (unknown size, could be quiet large) and combine them into one string and return them. If i just print the two strings then I get the correct result, but when I try…
vimalloc
  • 3,869
  • 4
  • 32
  • 45
3
votes
1 answer

Why does C's strcpy fail with doubly indexed arrays?

The following code seems to segfault and I cannot figure out why. #include static char src[] = "aaa"; int main() { char* target[2] = {"cccc","bbbbbbbbbb"}; strcpy(target[1],src); return 0; }
3
votes
4 answers

Is a CString always null-terminated?

From the CString interface, clearly one should not assume that a CString is null-terminated. However, it seems that sometimes there is, in fact, a null character at the end of the string. Is it possible, in the Windows implementation, to create a…
Tim Culver
  • 31
  • 1
  • 2
3
votes
5 answers

Pros and Cons of different string types in C++

Sorry to start another of those unanswerable questions on SO, but I'm just curious as to the pros and cons of all the different string types in C++. My particular question is between MFC's CStrings and std::string (since I'm doing Windows only…
bsruth
  • 5,372
  • 6
  • 35
  • 44
3
votes
5 answers

Remove the first part of a C String

I'm having a lot of trouble figuring this out. I have a C string, and I want to remove the first part of it. Let's say its: "Food,Amount,Calories". I want to copy out each one of those values, but not the commas. I find the comma, and return the…
Ethan Mick
  • 9,517
  • 14
  • 58
  • 74
3
votes
2 answers

Simple modification of C strings using pointers

I have two pointers to the same C string. If I increment the second pointer by one, and assign the value of the second pointer to that of the first, I expect the first character of the first string to be changed. For example: #include…
Kiwi
  • 1,083
  • 11
  • 26
3
votes
2 answers

C string one character shorter than defined length?

Very new to c++ and I have the following code: char input[3]; cout << "Enter input: "; cin.getline(input,sizeof(input)); cout << input; And entering something like abc will only output ab, cutting it one character short. The defined length is 3…
m0meni
  • 16,006
  • 16
  • 82
  • 141
3
votes
1 answer

What is the meaning of "Yes, Virginia, it had better be unsigned"?

In the linux source code version 3.18 (and previous), in the string.c file, in the function strncasecmp, the very first thing is: /* Yes, Virginia, it had better be unsigned */ unsigned char c1, c2; As can be seen here:…
Shelvacu
  • 4,245
  • 25
  • 44
3
votes
4 answers

Having issues with initializing character array

Ok, this is for homework about hashtables, but this is the simple stuff I thought I was able to do from earlier classes, and I'm tearing my hair out. The professor is not being responsive enough, so I thought I'd try here. We have a hashtable of…
quandrum
  • 1,608
  • 1
  • 11
  • 14
3
votes
2 answers

How to marshal the type of "Cstring" in .NET Compact Framework(C#)?

How to marshal the type of "Cstring" in .NET Compact Framework(C#)? DLLname:Test_Cstring.dll(OS is WinCE 5.0),source code: extern "C" __declspec(dllexport) int GetStringLen(CString str) { return str.GetLength(); } I marshal that in .NET Compact…
Smart_Joe
  • 63
  • 1
  • 7
3
votes
2 answers

C memory overlap?

I am trying to copy the first 16 bytes of a 32 byte string to dest. unsigned char src[32] = "HELLO-HELLO-HELLO-HELLO-HELLO-12"; unsigned char dest[16]; memcpy(dest, src, 16); // COPY printf("%s\n", src); printf("%lu\n",…
desktop
  • 145
  • 2
  • 7
3
votes
2 answers

Conversion from int to c-string (const char*) fails

I fail to convert int to a c-string (const char*): int filenameIndex = 1; stringstream temp_str; temp_str<<(fileNameIndex); const char* cstr2 = temp_str.str().c_str(); There is no error but cstr2 does not get the expected value. It is…
user3165438
  • 2,631
  • 7
  • 34
  • 54
3
votes
2 answers

CMutablePointer to String in swift-language

I have a library which has a function like this: int get_user_name(const char **buffer); in swift, should call like this: var name:CMutablePointer = nil get_user_name(name) I want make use this function more comfortable so I wrapped this…
galilio
  • 307
  • 1
  • 3
  • 13
3
votes
2 answers

C++ read a file line by line but line type is CString or TCHAR

I get the following example CString line[100]; //string line; ifstream myfile (_T("example.txt")); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << '\n'; } …
Myrda Sahyuti
  • 63
  • 3
  • 7