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

Correct way to define a constant C-string in C++?

Most of the time I see constant C-strings defined as: static char const* MY_CONSTANT = "Hello World"; However, the pointer itself is not const. Wouldn't it be more appropriate to do it like below? static char const* const MY_CONSTANT = "Hello…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
14
votes
5 answers

Cannot modify C string

Consider the following code. int main(void) { char * test = "abcdefghijklmnopqrstuvwxyz"; test[5] = 'x'; printf("%s\n", test); return EXIT_SUCCESS; } In my opinion, this should print abcdexghij. However, it just terminates without…
fresskoma
  • 25,481
  • 10
  • 85
  • 128
13
votes
2 answers

C++ LibCurl - Converting CURLcode into a CString

What would be the easiest way to convert the "res" variable (CURLcode) into a CString? Here's the standard example which compiles fine on my machine but I want to use this in a MFC app and display the result as a MessageBox. Any help is…
kogh
  • 995
  • 4
  • 17
  • 30
13
votes
3 answers

iPhone stringWithCString is deprecated

I use this code to read data from sqlite database: keyFromSql = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1)]; but the compiler give me the warning wrote in the title... so, what is the right and not deprecated…
ghiboz
  • 7,863
  • 21
  • 85
  • 131
13
votes
7 answers

string array with garbage character at end

I have a char array buffer that I am using to store characters that the user will input one by one. My code below works but has a few glitches that I can't figure out: when I execute a printf to see what's in Buffer, it does fill up but I get…
Steve
  • 1,955
  • 9
  • 28
  • 30
10
votes
3 answers

How to elegantly initialize vector with string literal?

The problem comes from an exercise on C++ Primer 5th Edition: Write a program to assign the elements from a list of char* pointers to C-style character strings to a vector of strings. ----------------Oringinal Question------------ First I try…
Warbean
  • 547
  • 2
  • 5
  • 19
9
votes
2 answers

How sprintf works with CString and std::string

CString s = "test"; std::string ss = "test"; char z[100]; sprintf(z, "%s", ss.c_str()); // z = "test" : OK char z2[100]; sprintf(z2, "%s", ss); // z2 = "(null)" : OK. undefined behavior is expected char z3[100]; sprintf(z3, "%s", s); // z3 =…
rmi
  • 532
  • 4
  • 15
8
votes
2 answers

Breaking down string and storing it in array

I want to break down a sentence and store each string in an array. Here is my code: #include #include int main(void) { int i = 0; char* strArray[40]; char* writablestring= "The C Programming Language"; char…
Adam Adamou
  • 243
  • 2
  • 6
  • 12
8
votes
5 answers

Convert CString to character array?

How to convert CString in MFC to char[] (character array)
yesraaj
  • 46,370
  • 69
  • 194
  • 251
8
votes
3 answers

C++ CString equivalent in C#

What is the C# equivalent for MFC's CString?
Thomas Anderson
  • 1,977
  • 7
  • 17
  • 22
8
votes
2 answers

C - How to read a string line by line?

In my C program, I have a string that I want to process one line at a time, ideally by saving each line into another string, doing what I want with said string, and then repeating. I have no idea how this would be accomplished, though. I was…
user1174511
  • 309
  • 3
  • 5
  • 15
8
votes
3 answers

Print out elements of an array of strings in c

I've created a function that takes a pointer to the first element in a C-String array. This is the array, and how I went about making it: char element1[60] = "ubunz"; char element2[60] = "uasdasffdnz"; char* array[10] = {element1,element2}; Then I…
turnt
  • 3,235
  • 5
  • 23
  • 39
8
votes
2 answers

How can I slice a string in C?

I need to find if a char array starts with "ADD". I know to use strcmp(), but I don't know how to get the first three characters. I really hate working with c-strings. How can I take a slice of a char array like char buffer[1024]?
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
8
votes
2 answers

How to check if a CString contains only numeric characters

I'm reading from a file and parsing its contents. I need to make sure a CString value consist of only numbers. What are the different methods i could achieve it? Sample code: Cstring Validate(CString str) { if(/*condition to check whether its all…
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
7
votes
7 answers

Simply encrypt a string in C

I'm trying to encrypt a query string on a game I'm making when opening a url. It doesn't have to be complicated, in fact since I'm working from a game engine it needs to be as simple as possible. It tends to fuss if I get too low level. I've…
Isaiah
  • 1,995
  • 2
  • 18
  • 29
1
2
3
32 33