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]
2715 questions
20
votes
3 answers
A proper way of associating enums with strings
Let's say I have a number of strings I use often throughout my program (to store state and things like that). String operations can be expensive, so whenever addressing them I'd like to use an enumeration. I've seen a couple solutions so…

PoVa
- 995
- 9
- 24
20
votes
2 answers
What language standards allow ignoring null terminators on fixed size arrays?
We are transitioning C code into C++.
I noticed that the following code is well defined in C,
int main(){
//length is valid. '\0' is ignored
char str[3]="abc";
}
as it is stated in Array initialization that:
"If the size of the array is…

Trevor Hickey
- 36,288
- 32
- 162
- 271
20
votes
3 answers
Comparing two char* for equality
Possible Duplicate:
What is the proper function for comparing two C-style strings?
My match condition doesn't work! Can someone advise how to compare to C-style strings?
void saveData(string line, char* data){
char *testString = new…

Bryan Wong
- 623
- 2
- 7
- 21
20
votes
18 answers
Why use c strings in c++?
Is there any good reason to use C-strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string.

Jason Baker
- 192,085
- 135
- 376
- 510
18
votes
4 answers
Are strtol, strtod unsafe?
It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string:
#include
#include
int main() {
const char *foo = "Hello, world!";
char *bar;
strtol(foo, &bar, 10); // or…

user102008
- 30,736
- 10
- 83
- 104
18
votes
4 answers
Print part of a string in C
Is there a way to only print part of a string?
For example, if I have
char *str = "hello there";
Is there a way to just print "hello", keeping in mind that the substring I want to print is variable length, not always 5 chars?
I know that I could…

Mark
- 501
- 2
- 5
- 5
18
votes
4 answers
converting c style string to c++ style string
Can anyone please tell me how to convert a C style string (i.e a char* ) to a c++ style string (i.e. std::string) in a C++ program?
Thanks a lot.

assassin
- 19,899
- 10
- 30
- 43
17
votes
4 answers
What if I don't call ReleaseBuffer after GetBuffer?
From CString to char*, ReleaseBuffer() must be used after GetBuffer(). But why? What will happen if I don't use ReleaseBuffer() after GetBuffer()?
Can somebody show me an example? Thanks.

Landy
- 379
- 1
- 4
- 10
16
votes
3 answers
Set Qt default encoding to UTF-8
In my Qt application my source code files are encoded as UTF-8. For the following code...
QMessageBox::critical(this, "Nepoznata pogreška", "Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?", QMessageBox::Yes,…

xx77aBs
- 4,678
- 9
- 53
- 77
16
votes
5 answers
When/Why is '\0' necessary to mark end of an (char) array?
So I just read an example of how to create an array of characters which represent a string.
The null-character \0 is put at the end of the array to mark the end of the array. Is this necessary?
If I created a char array:
char line[100];
and put…

Strict
- 195
- 1
- 2
- 9
16
votes
3 answers
In C, can I initialize a string in a pointer declaration the same way I can initialize a string in a char array declaration?
Do these two lines of code achieve the same result? If I had these lines in a function, is the string stored on the stack in both cases? Is there a strong reason why I should use one over the other, aside from not needing to declare the null…

aoeu
- 1,128
- 2
- 13
- 22
16
votes
6 answers
Why should one use std::string over c-style strings in C++?
"One should always use std::string over c-style strings(char *)" is advice that comes up for almost every source code posted here. While the advice is no doubt good, the actual questions being addressed do not permit to elaborate on the why? aspect…

Alok Save
- 202,538
- 53
- 430
- 533
14
votes
9 answers
How can I check if a string has special characters in C++ effectively?
I am trying to find if there is better way to check if the string has special characters. In my case, anything other than alphanumeric and a '_' is considered a special character. Currently, I have a string that contains special characters such as…

Praveen
- 1,781
- 6
- 26
- 32
14
votes
4 answers
How should character arrays be used as strings?
I understand that strings in C are just character arrays. So I tried the following code, but it gives strange results, such as garbage output or program crashes:
#include
int main (void)
{
char str [5] = "hello";
puts(str);
}
Why…

Lundin
- 195,001
- 40
- 254
- 396
13
votes
7 answers
How to write a better strlen function?
I am reading "Write Great Code Volume 2" and it shows the following strlen impelementation:
int myStrlen( char *s )
{
char *start;
start = s;
while( *s != 0 )
{
++s;
}
return s - start;
}
the book says that this…

Victor
- 1,655
- 9
- 26
- 38