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

Why can't I initialize an array of cstrings like this?

char sXSongBuffer[20][30]; sXSongBuffer = {"Thriller", "Don't Stop Till You Get Enough", "Billy Jean"}; Why does this return the error expected expression before ‘{’ token? The reason I want to initialize my array like this is so that I can change…
Pieter
  • 31,619
  • 76
  • 167
  • 242
3
votes
4 answers

Why is my char* writable and sometimes read only in C++

I have had really big problems understand the char* lately. Let's say I made a recursive function to revert a char* but depending on how I initialize it I get some access violations, and in my C++ primer I didn't find anything giving me the right…
DogDog
  • 4,820
  • 12
  • 44
  • 66
3
votes
3 answers

Why no strnspn() nor strncspn()?

There is a strnlen() function which has been added for some time to various high-profile libraries, and subsequently made its way into standards like POSIX-1:2008. IIUC its purpose is to avoid problems when the subject string is not…
AntoineL
  • 888
  • 4
  • 25
3
votes
2 answers

Inheriting from CString caused out of memory exception

I have the following string class that inherits from MFC CString class TString : public CString { public: TString() : CString(_T("")) { } TString(LPCTSTR str) : CString(str) { } }; I had an out of memory exception in a…
ahmedsafan86
  • 1,776
  • 1
  • 26
  • 49
3
votes
3 answers

Pass CString to fprintf

I have ran the code analyzer in visual studio on a large code base and i got about a billion of this error: warning C6284: Object passed as parameter '3' when string is required in call to 'fprintf' According to…
Simon Karlsson
  • 4,090
  • 22
  • 39
3
votes
2 answers

Return an empty C-String

Simple Question: How do you return an empty C-String with as little code as possible? I have code that needs to return an empty char*. I am looking for something along the lines of return "";. I know there are several ways to do this, but I am…
Evorlor
  • 7,263
  • 17
  • 70
  • 141
3
votes
4 answers

Properly using/creating dynamic cstrings in C with malloc?

The following code always segfaults: char *test3 = (char *) malloc(sizeof(char) * 5); test3 = "asdf"; printf("%s\n", test3); The following code does not segfault: char *test3 = (char *) malloc(sizeof(char) * 5); test3[0] = 'a'; test3[1]…
user1529891
3
votes
8 answers

Safely concatenate c-strings in class constructor

I have a class that needs to concatenate two const char* strings during construction, and even use the result (concatenated string) later on in the initialization list. const char* SUFFIX = "suffix"; class widget { public: widget(const char*…
Xlaudius
  • 1,757
  • 2
  • 13
  • 16
3
votes
1 answer

Use CString with sprintf

I have some C++ code where I need to use CString with sprintf. In this code I'm creating file names that are CStrings that are defined by sprintf. The code is below. double Number; Number = 0.25; char buffer [50]; CString sFile; sFile =…
3
votes
5 answers

Writing into c-string

my code segfaults and I don't know why. 1 #include 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x =…
Martin
  • 1,473
  • 2
  • 12
  • 20
3
votes
1 answer

cstring string; vs char string;

what is the difference between typedef struct node *node_ref; typedef char *cstring; struct node { cstring string; node_ref link; }; and typedef struct node *node_ref; struct node { char string; node_ref link; }; my program compiles fine…
paeynivek
  • 41
  • 2
3
votes
3 answers

Nested C array pointer types

This seems like a question that should have been answered already somewhere but I can't seem to find something satisfactory. Anyway I need to return from a function something that looks like: { {"foo", "bar"}, {"baz", "foo"}, {"foo", "bar"} } I am…
dreamriver
  • 1,291
  • 2
  • 15
  • 20
3
votes
8 answers

simple string runtime error in C?

This code compiles fine but give segmentation fault error while running? Can anyone tell why? #include #include #include int main() { const char s2[] = "asdfasdf"; char* s1; strcpy(s1, s2); …
Amol Aggarwal
  • 2,674
  • 3
  • 24
  • 32
3
votes
1 answer

Need help copying c-strings from embedded SQL fetch to another c-string in a separate struct

I've hit a wall with a program that uses embedded SQL to fetch rows from a database table, stores the row data in a struct, and then has that data processed with results being stored in another struct and pushed to a linked list. The struct that…
PSUlion01
  • 115
  • 2
  • 10
3
votes
3 answers

Array initialization without comma

In the following code I am having the output like(last one giving segmentation fault) U s HelloThisisatest Segmentation fault (core dumped) but i do not understand why. code is int main() { char *a[]={"Hello" "This" "is" "a"…
fuzzy
  • 229
  • 1
  • 4
  • 15