Questions tagged [c-strings]

A string in the programming language C is represented as a sequence of characters followed by a null terminator (represented as \0).

2715 questions
337
votes
19 answers

Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; // could be also written as *str = 'z' printf("%s\n", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s\n", str); Tested…
Markus
  • 3,491
  • 3
  • 18
  • 6
118
votes
7 answers

What is the lifetime of the result of std::string::c_str()?

In one of my programs, I have to interface with some legacy code that works with const char*. Lets say I have a structure which looks like: struct Foo { const char* server; const char* name; }; My higher-level application only deals with…
ereOn
  • 53,676
  • 39
  • 161
  • 238
91
votes
3 answers

Is it possible to print out only a certain section of a C-string, without making a separate substring?

Say I have the following: char* string = "Hello, how are you?"; Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printf that would allow for this?
Tim
  • 4,295
  • 9
  • 37
  • 49
91
votes
16 answers

How do you convert CString and std::string std::wstring to each other?

CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CStrings as keys, so I want to convert the CString into a std::string. Writing a CString hash function seems to…
user25749
  • 4,825
  • 14
  • 61
  • 83
66
votes
11 answers

What happens to memory after '\0' in a C string?

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset,…
Erika Electra
  • 1,854
  • 3
  • 20
  • 31
65
votes
8 answers

Is it bad to declare a C-style string without const? If so, why?

Doing this in C++ char* cool = "cool"; compiles fine, but gives me a warning: deprecated conversion from string constant to char*. I would never willfully use a C-style string over std::string, but just in case I'm asked this question: is it…
Coffee Maker
  • 1,543
  • 1
  • 16
  • 29
61
votes
4 answers

Using a C string gives a warning: "Address of stack memory associated with local variable returned"

I am not a C programmer, so I am not that familiar with C-string, but now I have to use a C library, so here is a shortened version of my code to demonstrate my problem: char** ReadLineImpl::my_completion () { char* matches[1]; matches[0] =…
khajvah
  • 4,889
  • 9
  • 41
  • 63
59
votes
6 answers

What is the difference between memcmp, strcmp and strncmp in C?

I wrote this small piece of code in C to test memcmp() strncmp() strcmp() functions in C. Here is the code that I wrote: #include #include #include int main() { char *word1="apple",*word2="atoms"; if…
und3rd06012
  • 717
  • 1
  • 14
  • 19
54
votes
4 answers

String termination - char c=0 vs char c='\0'

When terminating a string, it seems to me that logically char c=0 is equivalent to char c='\0', since the "null" (ASCII 0) byte is 0, but usually people tend to do '\0' instead. Is this purely out of preference or should it be a better…
Joe DF
  • 5,438
  • 6
  • 41
  • 63
48
votes
4 answers

Can a std::string contain embedded nulls?

For regular C strings, a null character '\0' signifies the end of data. What about std::string, can I have a string with embedded null characters?
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
42
votes
2 answers

Expand macros inside quoted string

Possible Duplicate: C Macros to create strings I have a function which accepts one argument of type char*, like f("string"); If the string argument is defined by-the-fly in the function call, how can macros be expanded within the string…
davide
  • 2,082
  • 3
  • 21
  • 30
39
votes
2 answers

C - split string into an array of strings

I'm not completely sure how to do this in C: char* curToken = strtok(string, ";"); //curToken = "ls -l" we will say //I need a array of strings containing "ls", "-l", and NULL for execvp() How would I go about doing this?
Jordan
  • 2,070
  • 6
  • 24
  • 41
36
votes
5 answers

Why do I first have to strcpy() before strcat()?

Why does this code produce runtime issues: char stuff[100]; strcat(stuff,"hi "); strcat(stuff,"there"); but this doesn't? char stuff[100]; strcpy(stuff,"hi "); strcat(stuff,"there");
Or Cyngiser
  • 1,027
  • 1
  • 12
  • 16
34
votes
3 answers

How to declare constexpr C string?

I think i quite understand how to use the keyword constexpr for simple variable types, but i'm confused when it comes to pointers to values. I would like to declare a constexpr C string literal, which will behave like #define my_str "hello" That…
Youda008
  • 1,788
  • 1
  • 17
  • 35
33
votes
7 answers

Why is strdup considered to be evil

I've seen some posters stating that strdup is evil. Is there a consensus on this? I've used it without any guilty feelings and can see no reason why it is worse than using malloc/memcpy. The only thing I can think might earn strdup a reputation is…
William Morris
  • 3,554
  • 2
  • 23
  • 24
1
2 3
99 100