3

I have two questions about char array.

  1. from the code bellow, since arr is const, why doesn't the compiler give me an error since I'm rewriting it?

    char arr[5]; // arr is a const pointer to (*)[5] array
    cin>>arr; //   
    
  2. when I initialized a char array like this:

    char arr[5]={'h','i'};
    

    if I did this:

    cout << arr << "something here \n"; 
    

    it will print hisomething here. I thought It should print out

    hi   something here
    

    with 3 witespaces.

    But if I did this:

    for(int i = 0; i < 5; i++){
      cout << arr[i];
    }
    

    it will printout the 3 whitespaces.

The second case seems to prove that the compiler doesn't add any null characters. So how can the compiler ignore the 3 whitespaces?

Shep
  • 7,990
  • 8
  • 49
  • 71
AlexDan
  • 3,203
  • 7
  • 30
  • 46

4 Answers4

8
  1. This array is not const, because there is no const qualifier.
  2. If you don't specify remaining values in initializer list, they will be initialized to 0. 0 is used to terminate C strings, not as a whitespace.

As for your claim, that for(int i=0;i<5;i++){ cout << arr[i]; } printed whitespace - how did you checked that?

For me:

#include <iostream>

int main(){
    char arr[5]={'h','i'};
    for(int i=0;i<5;i++){ std::cout << arr[i]; }
    std::cout << "X" << std::endl;
}

prints:

hiX

and hexdumped:

$ ./t | hexdump -Cv
00000000  68 69 00 00 00 58 0a                              |hi...X.|
00000007

There are '\0' chars printed. Their display seems to be operating system dependent. But they are not a whitespace.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • if they will be intialized to 0, then why in the second code, they got treated as whitespace? – AlexDan Apr 21 '12 at 08:23
  • 1
    Why do you think they get treated as whitespaces? Try redirecting the output to a text file and then examine that with a hex editor. – Mr Lister Apr 21 '12 at 08:26
  • @AlexDan: Then your system displays the null character as a whitespace. However, it's still a null character. – Mooing Duck Apr 21 '12 at 09:08
  • @MooingDuck : can you tell me the difference between '\0' and "\0" bcz the first display as a whitespace and the second doesn't. – AlexDan Apr 21 '12 at 09:22
  • 1
    @AlexDan That is not caused by how they are stored, but by how you print them. `cout` differentiates between characters and strings. Printing a character outputs one character, no matter if it's `\0` or not. Printing a string outputs all characters in the string up to, but not including, the first `\0`. – Mr Lister Apr 21 '12 at 17:10
3
  1. arr itself is a "char[5]" not a "const char (*)[5]". And it is implicitly cast to a "char *" rvalue when you write cin >> arr. It's not a const because it is not even a lvalue.

And "const char *" or "char const *" mean the lvalue pointed to cannot be changed, while "char * const" means the pointer lvalue itself cannot be change. But this has nothing to do with you question, though.

  1. First, there was no whitespace. And it is cin appended the null character.

An array is just an array:

char a[5]; //a's type is char[5];

But an array can hardly be an operand. Only operators I can remember that accept array type are sizeof and &(address-of) (On the other hand, this means sometimes a have to be an array. Or else if you write sizeof(a), it will give you the size of a pointer.). For other operations a is converted to a char * rvalue. And yes, even when you write a[0], a[1], etc. a[0] is equivalent to *(a + 0), which operates on pointers but not arrays.

When you cannot assign to something, it doesn't always mean that thing is "const":

  1. You cannot assign to a const variable of course.
  2. You can only assign to a variable(or aka lvalue), so you cannot assign something to a rvalue(or aka value). So you cannot write 1 = 2, because 1 is an rvalue not a variable, not because 1 is "const".
  3. You must assign something to a variable that matches its type. So if you have a const char *p and a char *q, you cannot write q = p. Their types don't match. And again, it doesn't mean q is const, for it's obviously not. But you can write p = q, because char * can be implicitly cast to const char *. But const char * have to be cast to char * explicitly.
BlueWanderer
  • 2,671
  • 2
  • 21
  • 36
  • arr is a const pointer, so you cant do this arr="something". but you are saying the you can do this cin>>arr; e.g. I have cin>>arr; and I input "cpp"; is this will be replaced by arr[0]='c',arr[1]='p',arr[2]='p'; – AlexDan Apr 21 '12 at 08:40
  • 1
    You cannot write arr = "" because arr is an array not a pointer. Even if implicitly casted to a pointer, arr is a pointer rvalue, which cannot be assigned. "cpp" part: no, you missed arr[3] = '\0'; – BlueWanderer Apr 21 '12 at 08:45
  • About the const pointer thing: if arr were a const char *, you wouldn't be able to write char *x = arr, but in fact you can. – BlueWanderer Apr 21 '12 at 08:47
  • doing char*x=arr; doesn't have anything to do with arr being const or not. try char &x=arr; you will get an error. since arr is const. – AlexDan Apr 21 '12 at 08:50
  • 1
    Const pointer cannot be assigned to non-const pointer directly. char &x = arr, well, an army can never be referred as a person. I suggest you do some reading first, you are believing in a lot of things that are wrong. – BlueWanderer Apr 21 '12 at 08:53
  • I think I got what you are trying to say. I was wrong. thanks – AlexDan Apr 21 '12 at 10:26
0

arr acts a const pointer which means that you cannot set the pointer to another address. It is not a pointer to a const so you can change the data at the address the pointer points to.

Example:

arr=&another_variable; //Illegal
*arr='A';              //Legal
user877329
  • 6,717
  • 8
  • 46
  • 88
  • yes I know that, but e.g. cin>>arr; and I typed "cpp". does the compiler will treate it like this 1) arr="cpp" or like this 2) arr[0]='c',arr[1]='p',arr[2]='p'. – AlexDan Apr 21 '12 at 08:30
  • It will treat it like: arr[0]='c',arr[1]='p',arr[2]='p' – user877329 Apr 22 '12 at 10:42
0
  1. the first it is not const. the second, this code is not safe.
  2. u should to setup termination symbol '\0' in the end.

You should not use a char type, try to use string instead char array.

Ilya Shcherbak
  • 131
  • 2
  • 11