2

I want to have \0 as one of my array (char type) element in my C/C++ code but not the last one. I am doing this.

char arr[10]  ; //array declaration
//initializing the array
arr[0] = 'a'  ;
arr[1] = '\0' ;
arr[2] = 's'  ;

When i try printing these array elements, i get some symbolic stuff as output for a[1]. I guess 0 is converted to char. Is that so ? If yes, can't i have \0 anyhow in my array before the termination ?

13
  • 2
    I'm guessing that you are either compiling C or compiling C++. Pick one to write, pick one to tag your question with. The languages can be quite different, and more so with more modern versions. Commented Dec 5, 2013 at 16:56
  • @crashmstr In this case both are valid. Why pick one, when you get exactly the same results in both? Usually I make exactly the same comment as you, but here it is actually appropriate to tag both. Commented Dec 5, 2013 at 16:57
  • "I guess 0 is converted to char". That already happened when you stored it in a char array. Commented Dec 5, 2013 at 16:57
  • @crashmstr But that won't make a difference in my case. Commented Dec 5, 2013 at 16:58
  • 3
    '\0' is not converted to char, it is a char (at least in C++) Commented Dec 5, 2013 at 17:01

4 Answers 4

3

'/0' is not a null character. Escaping uses a backslash: '\0'.

Sign up to request clarification or add additional context in comments.

Comments

3

You can have \0 in an array as same as other values and it's OK.

The problem is you can not print it due to two reasons:

  1. It terminates printing in cout and printf because they assume \0 means end of the string.

  2. \0 is not a printable character.

So, you should write your own function to print the array that prints something as \0 and doesn't terminate the string when reaches to it. (So, you should indicate the end of string by something else)

For example:

void print(const std::string &s)
{
    for (auto x : s)
        std::cout << ((x == '\0')? '.' : x);
}

int main( )
{
    std::string s {'a','\0','s'};
    print(s);
}

Output:

a.s

It prints a dot instead of that non-printable value.

Note: The OP was tagged also.

Comments

2

You could write a function like this:

void print(char *arr, int length){
    for(int i = 0; i < length; i++){
        if(arr[i] != '\0')
           printf("%c",arr[i]);
        else
           printf("\\0");
    }
}

Comments

1

It's perfectly valid to have a '\0' in your (char) array but all the standard functions assume that it indicates the end of the array. So to print a (non-c-)string like this you need your own function. For example:

void myPuts(size_t length, char * str){
    size_t i;
    for(i = 0; i < length; i++){
        putchar(str[i]);
    }
}

and call it like this:

myPuts(10, arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.