Questions tagged [null-terminated]

A string that ends with (and does not include) a "null" (ASCII 0) byte, as used by the C language

The C language doesn't have a built-in string type, instead strings are represented by a contiguous sequence of characters followed by a null-terminator (a character with value zero) and are referred to by a pointer to the first character of the sequence. The length of the string is therefore implied by the position of the null terminator, rather than being stored explicitly. Operations on null-terminated strings use a pointer to the sequence and interpret all the characters up to the null-terminator as the contents of the string.

This contrasts with string types in other languages which might be represented by an abstract data type or as a pointer to data where the length is stored in one or more bytes at the start of the string.

206 questions
302
votes
20 answers

What's the rationale for null terminated strings?

As much as I love C and C++, I can't help but scratch my head at the choice of null terminated strings: Length prefixed (i.e. Pascal) strings existed before C Length prefixed strings make several algorithms faster by allowing constant time length…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
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
53
votes
3 answers

Will std::string always be null-terminated in C++11?

In a 2008 post on his site, Herb Sutter states the following: There is an active proposal to tighten this up further in C++0x and require null-termination and possibly ban copy-on-write implementations, for concurrency-related reasons. Here’s the…
links77
  • 779
  • 2
  • 7
  • 8
52
votes
2 answers

Are char * argv[] arguments in main null terminated?

So I'm wondering if command line parameters are always null terminated? Google seems to say yes, and compiling on GCC indicates this is the case, but can I guarantee this to always be true? int main(int argc, char** argv) { char *p; for(int…
LeviX
  • 3,096
  • 3
  • 28
  • 41
51
votes
12 answers

Copying non null-terminated unsigned char array to std::string

If the array was null-terminated this would be pretty straight forward: unsigned char u_array[4] = { 'a', 's', 'd', '\0' }; std::string str = reinterpret_cast(u_array); std::cout << "-> " << str << std::endl; However, I wonder what is the…
karlphillip
  • 92,053
  • 36
  • 243
  • 426
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
43
votes
7 answers

What is a null-terminated string?

How does it differ from std::string?
lhj7362
  • 2,173
  • 4
  • 19
  • 17
36
votes
10 answers

Why null-terminated strings? Or: null-terminated vs. characters + length storage

I'm writing a language interpreter in C, and my string type contains a length attribute, like so: struct String { char* characters; size_t length; }; Because of this, I have to spend a lot of time in my interpreter handling this kind of…
Imagist
  • 18,086
  • 12
  • 58
  • 77
34
votes
4 answers

strstr() for a string that is NOT null-terminated

How do I do the in-place equivalent of strstr() for a counted string (i.e. not null-terminated) in C?
user541686
  • 205,094
  • 128
  • 528
  • 886
29
votes
3 answers

Warning comparison between pointer and integer

I am getting a warning when I iterate through the character pointer and check when the pointer reaches the null terminator. const char* message = "hi"; //I then loop through the message and I get an error in the below if statement. if…
catee
  • 401
  • 1
  • 4
  • 4
27
votes
6 answers

How can a file contain null bytes?

How is it possible that files can contain null bytes in operating systems written in a language with null-terminating strings (namely, C)? For example, if I run this shell code: $ printf "Hello\00, World!" > test.txt $ xxd test.txt 0000000: 4865…
RK.
  • 867
  • 2
  • 10
  • 19
25
votes
4 answers

Using std::string_view with api that expects null-terminated string

I have a method that takes std::string_view and uses function, which takes null terminated string as parameter. For example: void stringFunc(std::experimental::string_view str) { some_c_library_func(/* Expects null terminated string */); } The…
Schtolc
  • 1,036
  • 1
  • 12
  • 19
24
votes
3 answers

Null character in strings

Consider this string: var s = "A\0Z"; Its length is 3, as given by s.length. Using console.log you can see the string isn't cut and that s[1] is "" and s.charCodeAt(1) is 0. When you alert it in Firefox, you see AZ. When you alert it in…
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
24
votes
6 answers

C++ char array null terminator location

I am a student learning C++, and I am trying to understand how null-terminated character arrays work. Suppose I define a char array like so: char* str1 = "hello world"; As expected, strlen(str1) is equal to 11, and it is null-terminated. Where does…
John Mahoney
  • 343
  • 1
  • 2
  • 6
23
votes
9 answers

Why do strings in C need to be null terminated?

Just wondering why this is the case. I'm eager to know more about low level languages, and I'm only into the basics of C and this is already confusing me. Do languages like PHP automatically null terminate strings as they are being interpreted and /…
alex
  • 479,566
  • 201
  • 878
  • 984
1
2 3
13 14