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
23
votes
2 answers

[My]SQL VARCHAR Size and Null-Termination

Disclaimer: I'm very new to SQL and databases in general. I need to create a field that will store a maximum of 32 characters of text data. Does "VARCHAR(32)" mean that I have exactly 32 characters for my data? Do I need to reserve an extra…
Vulcanus
22
votes
4 answers

How to get a null terminated string from a C# string?

I am communicating with a server who needs null terminated string How can I do this smartly in C#?
Betamoo
  • 14,964
  • 25
  • 75
  • 109
21
votes
5 answers

Why are C#/.NET strings length-prefixed and null terminated?

After reading What's the rationale for null terminated strings? and some similar questions I have found that in C#/.NET strings are, internally, both length-prefixed and null terminated like in BSTR Data Type. What is the reason strings are both…
prostynick
  • 6,129
  • 4
  • 37
  • 61
20
votes
5 answers

Why are strings in C++ usually terminated with '\0'?

In many code samples, people usually use '\0' after creating a new char array like this: string s = "JustAString"; char* array = new char[s.size() + 1]; strncpy(array, s.c_str(), s.size()); array[s.size()] = '\0'; Why should we use '\0' here?
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
17
votes
8 answers

How to memset char array with null terminating character?

What is the correct and safest way to memset the whole character array with the null terminating character? I can list a few usages: ... char* buffer = new char [ARRAY_LENGTH]; //Option 1: memset( buffer, '\0', sizeof(buffer)…
Koray
  • 467
  • 1
  • 5
  • 12
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

Range based for loops on null terminated strings

I sort of assumed that range based for loops would support C-style strings void print_C_str(const char* str) { for(char c : str) { cout << c; } } However this is not the case, the standard [stmt.ranged] (6.5.4) says that…
Motti
  • 110,860
  • 49
  • 189
  • 262
14
votes
3 answers

Are all char arrays automatically null-terminated?

Probably I'm just too dump for googling, but I always thought char arrays get only null terminated by an literal initialization (char x[]="asdf";) and got a bit surprised when I saw that this seems not to be the case. int main() { char x[2]; …
user1329846
  • 171
  • 1
  • 1
  • 7
13
votes
5 answers

Null-terminate string: Use '\0' or just 0?

If I need to null-terminate a String, should I rather use \0 or is a simple 0 also enough? Is there any difference between using char a[5]; a[0] = 0; and char a[5]; a[0] = '\0'; Or is \0 just preferred to make it clear that I'm null-terminating…
erg
  • 1,632
  • 1
  • 11
  • 23
13
votes
7 answers

string array with garbage character at end

I have a char array buffer that I am using to store characters that the user will input one by one. My code below works but has a few glitches that I can't figure out: when I execute a printf to see what's in Buffer, it does fill up but I get…
Steve
  • 1,955
  • 9
  • 28
  • 30
11
votes
3 answers

What are the specifics of the definition of a string in C?

I am supposed to answer a homework question for one of my classes. Specifically, I am supposed to say if certain arrays in C are considered strings or not. Based on this article (https://www.geeksforgeeks.org/strings-in-c-2/) I know that strings are…
quango
  • 137
  • 8
10
votes
6 answers

Non null-terminated string compiler option for gcc

Update turns out this is just another case of "c++ is not c blues" What I want const char hex[16] = "0123456789ABCDEF"; the only thing that works char hex[16] = "0123456789ABCDE"; hex[15] = "F"; are there any compiler options or something I can…
GlassGhost
  • 16,906
  • 5
  • 32
  • 45
10
votes
1 answer

Special characters \0 {NUL} in Java

How to replace \0 (NUL) in the String? String b = "2012yyyy06mm"; // sth what i want String c = "2\0\0\0012yyyy06mm"; String d = c.replaceAll("\\\\0", ""); // not work String e = d.replace("\0", ""); // er, the…
user1900556
  • 109
  • 1
  • 1
  • 5
9
votes
3 answers

Do std::strings end in '\0' when initialized with a string literal?

I know string objects aren't null terminated but why should this work? std::string S("Hey"); for(int i = 0; S[i] != '\0'; ++i) std::cout << S[i]; So the constructor copies the null terminator as well, but does not increment the length? Why does…
nek28
  • 259
  • 1
  • 2
  • 6
9
votes
5 answers

C++: Store read binary file into buffer

I'm trying to read a binary file and store it in a buffer. The problem is, that in the binary file are multiple null-terminated characters, but they are not at the end, instead they are before other binary text, so if I store the text after the '\0'…
schacker22
  • 439
  • 2
  • 5
  • 12
1
2
3
13 14