Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a null-terminated string?

How does it differ from std::string?

like image 662
lhj7362 Avatar asked Jan 10 '10 14:01

lhj7362


People also ask

What is null-terminated character string?

The null character indicates the end of the string. Such strings are called null-terminated strings. The null terminator of a multibyte string consists of one byte whose value is 0. The null terminator of a wide-character string consists of one gl_wchar_t character whose value is 0.

What is a null-terminated string C++?

The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by '\0'). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.

Why are strings null-terminated?

The reason you need a null terminator on your string is because once it is broken down into assembly language each character gets a byte of sequential logical memory allocated to it in the stack in the main memory (RAM) and that is what the computer looks for to know 2 things.

What do we mean by a null terminator?

The null character (also null terminator) is a control character with the value zero. It is present in many character sets, including those defined by the Baudot and ITA2 codes, ISO/IEC 646 (or ASCII), the C0 control code, the Universal Coded Character Set (or Unicode), and EBCDIC.


1 Answers

A "string" is really just an array of chars; a null-terminated string is one where a null character '\0' marks the end of the string (not necessarily the end of the array). All strings in code (delimited by double quotes "") are automatically null-terminated by the compiler.

So for example, "hi" is the same as {'h', 'i', '\0'}.

like image 166
Ricket Avatar answered Sep 23 '22 18:09

Ricket