A string in the programming language C is represented as a sequence of characters followed by a null terminator (represented as \0).
Questions tagged [c-strings]
1958 questions
308
votes
17 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…
![](../../users/profiles/24622.webp)
Markus
- 3,201
- 3
- 16
- 6
104
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…
![](../../users/profiles/279259.webp)
ereOn
- 48,328
- 33
- 147
- 228
79
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…
![](../../users/profiles/25749.webp)
user25749
- 4,489
- 13
- 57
- 83
77
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?
![](../../users/profiles/831923.webp)
Tim
- 4,015
- 8
- 32
- 47
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,…
![](../../users/profiles/1237053.webp)
Erika Electra
- 1,789
- 3
- 18
- 30
63
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…
![](../../users/profiles/2817885.webp)
Coffee Maker
- 1,455
- 15
- 26
52
votes
3 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…
![](../../users/profiles/883015.webp)
Joe DF
- 4,909
- 6
- 34
- 54
49
votes
5 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…
![](../../users/profiles/1355630.webp)
und3rd06012
- 617
- 1
- 13
- 16
44
votes
4 answers
Using C-string gives 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] =…
![](../../users/profiles/1527217.webp)
khajvah
- 4,049
- 8
- 34
- 56
41
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?
![](../../users/profiles/115751.webp)
WilliamKF
- 36,283
- 61
- 170
- 271
35
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…
![](../../users/profiles/1012773.webp)
davide
- 1,896
- 3
- 19
- 28
34
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?
![](../../users/profiles/1427146.webp)
Jordan
- 1,920
- 5
- 20
- 41
29
votes
6 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…
![](../../users/profiles/1163625.webp)
William Morris
- 3,294
- 1
- 20
- 23
28
votes
4 answers
Strip first and last character from C string
I have a C string that looks like "Nmy stringP", where N and P can be any character. How can I edit it into "my string" in C?
![](../../users/profiles/173781.webp)
igul222
- 8,327
- 14
- 47
- 58
27
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");
![](../../users/profiles/962538.webp)
Or Cyngiser
- 867
- 1
- 9
- 16