How are null-terminated strings terminated in C++11?

NPS

Maybe it's stupid or obvious but I couldn't google any answer. What character ends a null-terminated string in C++11? NULL (which is in fact 0) or new nullptr? On the one hand, nullptr is supposed to replace NULL. On the other, though, I'm not sure if nullptr is a character at all. Or can be interpreted as one.

Filip Roséen - refp

NULL and nullptr has little to do with null-terminated strings. Both NULL and nullptr are used to denote a pointer which points to nothing, ie. null.

The null-termination of c-style strings is still (and has always) been denoted by a CharT having the integral value 0; or as it's most often written when talking, through a char-literal; '\0'.

Remember that character types are nothing more than integral types with some special meaning.

Comparing a char to an int (which is the type of literal 0) is allowed, it's also allowed to assign the value 0 to a char, as stated: a character type is an integral type.. and integral types hold integral values.


Why this confusion?

Back in the days when we didn't have nullptr, instead we had the macro NULL to denote that a certain pointer didn't have anything to point towards. The value of NULL is, and was, implementation-specific but the behaviour was well-defined; it shall not compare equal to any pointer value that is actually pointing to something.

As a result of how the behaviour of NULL was described plenty of compilers used #define NULL 0, or similar construct, resulting in a "feature" where one could easily compare NULL to any integral type (including char) to see if it's relation to the value zero.


With the previously stated in mind you'd often stumbled upon code such as the below, where the for-condition would be equivalent of having *ptr != 0.

char const * str = "hello world";

for (char const * ptr = str; *ptr != NULL; ++ptr) {
  ...    
}

Lesson learned: Just because something works doesn't mean that it is correct...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How are null-terminated strings terminated in C++11?

From Dev

How are strings terminated in C#?

From Dev

Encoding and null terminated strings

From Dev

Encoding and null terminated strings

From Dev

How is this null terminated?

From Dev

Where are the null-terminated strings when converting from C to assembly?

From Dev

How a C string pointer within a string is null-terminated?

From Dev

C++ Combining two zero terminated strings?

From Dev

Length of null terminated string in C/C++

From Dev

C - why is only char array null terminated?

From Dev

Non null terminated array of characters in c++

From Dev

NULL-terminated array of struct types in C

From Dev

C++ ZLib GZipStream Decompression NULL terminated

From Dev

c++ null terminated array of objects

From Dev

How to convert a null terminated string to string?

From Dev

How to return a vector of null terminated const char*?

From Dev

Will C++11 std::string::operator[] return null-terminated buffer

From Dev

How to write a dataset of Null Terminated Fixed Length Strings with h5py

From Dev

How to write a dataset of Null Terminated Fixed Length Strings with h5py

From Dev

Not Null Terminated character array

From Dev

OLE bstr not null terminated?

From Dev

Is wstring null terminated?

From Dev

String is not null terminated error

From Dev

OLE bstr not null terminated?

From Dev

strlen sometimes equal to sizeof for null-terminated strings

From Dev

Section .shstrtab not having flag for containing null-terminated strings

From Dev

This function does not properly handle non-NULL terminated strings

From Dev

Convert Null Terminated Byte strings to Raw String Literal

From Dev

C++ Debug Assertation Failed: "String is not null terminated"

Related Related

  1. 1

    How are null-terminated strings terminated in C++11?

  2. 2

    How are strings terminated in C#?

  3. 3

    Encoding and null terminated strings

  4. 4

    Encoding and null terminated strings

  5. 5

    How is this null terminated?

  6. 6

    Where are the null-terminated strings when converting from C to assembly?

  7. 7

    How a C string pointer within a string is null-terminated?

  8. 8

    C++ Combining two zero terminated strings?

  9. 9

    Length of null terminated string in C/C++

  10. 10

    C - why is only char array null terminated?

  11. 11

    Non null terminated array of characters in c++

  12. 12

    NULL-terminated array of struct types in C

  13. 13

    C++ ZLib GZipStream Decompression NULL terminated

  14. 14

    c++ null terminated array of objects

  15. 15

    How to convert a null terminated string to string?

  16. 16

    How to return a vector of null terminated const char*?

  17. 17

    Will C++11 std::string::operator[] return null-terminated buffer

  18. 18

    How to write a dataset of Null Terminated Fixed Length Strings with h5py

  19. 19

    How to write a dataset of Null Terminated Fixed Length Strings with h5py

  20. 20

    Not Null Terminated character array

  21. 21

    OLE bstr not null terminated?

  22. 22

    Is wstring null terminated?

  23. 23

    String is not null terminated error

  24. 24

    OLE bstr not null terminated?

  25. 25

    strlen sometimes equal to sizeof for null-terminated strings

  26. 26

    Section .shstrtab not having flag for containing null-terminated strings

  27. 27

    This function does not properly handle non-NULL terminated strings

  28. 28

    Convert Null Terminated Byte strings to Raw String Literal

  29. 29

    C++ Debug Assertation Failed: "String is not null terminated"

HotTag

Archive