I\'m currently writing a little program but I keep getting this error when compiling
error: empty character constant
I realize i
To represent the fact that the value is not present you have two choices:
1) If the whole char
range is meaningful and you cannot reserve any value, then use char*
instead of char
:
char** c = new char*[N];
c[0] = NULL; // no character
*c[1] = ' '; // ordinary character
*c[2] = 'a'; // ordinary character
*c[3] = '\0' // zero-code character
Then you'll have c[i] == NULL
for when character is not present and otherwise *c[i]
for ordinary characters.
2) If you don't need some values representable in char
then reserve one for indicating that value is not present, for example the '\0'
character.
char* c = new char[N];
c[0] = '\0'; // no character
c[1] = ' '; // ordinary character
c[2] = 'a'; // ordinary character
Then you'll have c[i] == '\0'
for when character is not present and ordinary characters otherwise.
There is no such thing as the "empty character" ''
.
If you need a space character, that can be represented as a space: c[i] = ' '
or as its ASCII octal equivalent: c[i] = '\040'
. If you need a NUL character that's c[i] = '\0'
.
The empty space char would be ' '
. If you're looking for null that would be '\0'
.
String before = EMPTY_SPACE+TAB+"word"+TAB+EMPTY_SPACE;
String after = before.replaceAll(" ", "").replace('\t', '\0');
means after = "word"
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.
There are two ways to do the same instruction, that is, an empty string. The first way is to allocate an empty string on static memory:
char* my_variable = "";
or, if you want to be explicit:
char my_variable = '\0';
The way posted above is only for a character. And, the second way:
#include <string.h>
char* my_variable = strdup("");
Don't forget to use free() with this one because strdup() use malloc inside.