How does one represent the empty char?

前端 未结 9 1352
闹比i
闹比i 2020-12-22 18:06

I\'m currently writing a little program but I keep getting this error when compiling

error: empty character constant

I realize i

9条回答
  •  醉话见心
    2020-12-22 18:12

    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.

提交回复
热议问题