You can use c[i]= '\0' or simply c[i] = (char) 0.
The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.
Answer from ardent on Stack OverflowYou can use c[i]= '\0' or simply c[i] = (char) 0.
The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.
You can't store "no character" in a character - it doesn't make sense.
As an alternative you could store a character that has a special meaning to you - e.g. null char '\0' - and treat this specially.
Is there problem to declare an empty char array?
Replace char in C (including empty char)
Empty character constant in C++ - Stack Overflow
Empty value detection for char* type variable
This line:
m_pchString[m_nLength-1] = '';
What you probably mean is:
m_pchString[m_nLength-1] = '\0';
Or even:
m_pchString[m_nLength-1] = 0;
Strings are zero terminated, which is written as a plain 0 or the null character '\0'. For double quote strings "" the zero termintation character is implicitly added to the end, but since you explicitly set a single character you must specify which.
You've said that you "get an error stating that strncpy is unsafe to use if I use the null terminator," but you use strlen, which simply does not work if the string isn't null terminated. From cplusplus:
The length of a C string is determined by the terminating null-character
My suggestion would be to use null or 0 like others are suggesting and then just use strcpy instead of strncpy as you copy the whole string every time anyway.
Can we put it in C++ source code?
No, it would be a syntax error.
If not, what's the usage of ''?
There is no usage, unless your purpose is to cause a compilation error (for which there are probably better alternatives such as static_assert).
Can it be understood that empty character constant '' is just a pure grammar error just like a variable being named as 2018ch ?
Yes. The grammar says:
character-literal: encoding-prefix opt ' c-char-sequence '
Notice that unlike the encoding-prefix, c-char-sequence is not optional.
Side note: Yes, it is a character sequence - multi character literals exist. But you don't need to learn about them yet other than knowing that you probably won't need them. Just don't assume that they're strings.
'' makes no sense and thus it won't compile, what value is it supposed to have?
Remember, it's all just bits and bytes in memory at some point so what value should the bytes have that represent ''?
char a = 0;
//or
char a = '\0';
These represent "empty" chars which is the closest you'll get to ''.
You could use a non-printable character but they may end up looking like something else.
You'd be better off using strings:
printf("There %s %d neighbor%s\n",
nbNeighbors != 1 ? "are" : "is",
nbNeighbors,
nbNeighbors != 1 ? "s" : ""
);
OP: "I want to use the ternary operator..."
Unless the exercise is to practice the ternary operator, committing to a particular technique blinds one to possibilites.
The example below uses 'branchless' code to deliver a grammatically correct result (ignoring the American spelling of "neighbour".)
printf( "There %s %d neighbor%s\n",
"are\0is" + (nbNeighbors == 1)*4,
nbNeighbors,
"s" + (nbNeighbors == 1) );
I'd never recommend manipulating the truth, but manipulating a truth value can come in handy sometimes.
EDIT
The above may be difficult for some to read/understand.
Below is the same thing expressed slightly differently:
printf( "There %s %d neighbor%s\n",
!(nbNeighbors-1)*4 + "are\0is",
nbNeighbors,
!(nbNeighbors-1) + "s" );
Tested, functional and branchless.