Your line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)
Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).
Note: do not confuse your declaration with the statement in line 3 here
char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */
which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.
Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)
Answer from Jens on Stack OverflowYour line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)
Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).
Note: do not confuse your declaration with the statement in line 3 here
char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */
which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.
Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)
No, in this case you're pointing to a real (non-null) string with a length of 0. You can simply do the following to set it to actual null:
char* str = NULL;
Videos
The things that are called "C strings" will be null-terminated on any platform. That's how the standard C library functions determine the end of a string.
Within the C language, there's nothing stopping you from having an array of characters that doesn't end in a null. However you will have to use some other method to avoid running off the end of a string.
Determination of the terminating character is up to the compiler for literals and the implementation of the standard library for strings in general. It isn't determined by the operating system.
The convention of NUL termination goes back to pre-standard C, and in 30+ years, I can't say I've run into an environment that does anything else. This behavior was codified in C89 and continues to be part of the C language standard (link is to a draft of C99):
- Section 6.4.5 sets the stage for
NUL-terminated strings by requiring that aNULbe appended to string literals. - Section 7.1.1 brings that to the functions in the standard library by defining a string as "a contiguous sequence of characters terminated by and including the first null character."
There's no reason why someone couldn't write functions that handle strings terminated by some other character, but there's also no reason to buck the established standard in most cases unless your goal is giving programmers fits. :-)
I learned in C that a string ends with a null value, "\0". How do I print out this null value in C?
I tried doing this by scanning the string "paint". However, it doesn't seem to work -
```
#include <stdio.h>
int main() {
char name[100];
scanf("%s", name);
printf("The name is %c", name[5]);
}
```
This is my output -
```
paint
The name is some weird symbol looking like 0
Process finished with exit code 0
```
If data() returns a reference, it needs to append the underlying array with a null terminator, in which case it may need to perform a reallocation which modifies the pointer in the string and data()’s implementation cannot be const.
If data() returns a copy, then this is a major difference between string and vector<char> and I’ve learned something.
I’m not at my laptop so can’t write a simple program where I modify the array returned from data() then print the string.
Don't all strings get initialized as an empty string? And if they get initialized as null...why... What kind of design decision is that.
The editor forces me to use string? temp = Console.ReadLine()
because if I use string it's possible it can be null. How?? Even if they just press enter it still would just be an empty string. An empty string isn't null, it's just empty. I don't see how someone could pass the program null as a value.
EDIT: Case closed
I can't assign a null to a String?
No. std::string is not a pointer type; it cannot be made "null." It cannot represent the absence of a value, which is what a null pointer is used to represent.
It can be made empty, by assigning an empty string to it (s = "" or s = std::string()) or by clearing it (s.clear()).
You cannot assign NULL or 0 to a C++ std::string object, because the object is not a pointer. This is one key difference from C-style strings; a C-style string can either be NULL or a valid string, whereas C++ std::strings always store some value.
There is no easy fix to this. If you'd like to reserve a sentinel value (say, the empty string), then you could do something like
const std::string NOT_A_STRING = "";
mValue = NOT_A_STRING;
Alternatively, you could store a pointer to a string so that you can set it to null:
std::string* mValue = NULL;
if (value) {
mValue = new std::string(value);
}
Hope this helps!