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.
Replace char in C (including empty char)
Empty character constant in C++ - Stack Overflow
Empty value detection for char* type variable
Empty Character Literal - Post.Byes - Bytes
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 ''.
I am writing a code that requires the use of char arrays in a loop and we are not allowed to use string functions. What is the best way to 'clean' the char arrays (for lack of a better word) so that they are 'fresh' the next round of the loop?
Do the char arrays hold C strings? If so, just set the first item to '\0'.
Otherwise, just write your own memset, which means looping over the array and set each item to '\0'. After which you go to your professor and tell them off for their arbitrary, counterproductive, artificial restrictions.
memset() is not a 'string function'. Also, what does 'fresh' mean? If you don't like the values in it, overwrite them or just don't read them.
using
memset(members, 0, 255);
in general
memset(members, 0, sizeof members);
if the array is in scope, or
memset(members, 0, nMembers * (sizeof members[0]) );
if you only have the pointer value, and nMembers is the number of elements in the array.
EDIT Of course, now the requirement has changed from the generic task of clearing an array to purely resetting a string, memset is overkill and just zeroing the first element suffices (as noted in other answers).
EDIT In order to use memset, you have to include string.h.
Depends on what you mean by 'empty':
members[0] = '\0';