arrays

Hi
Java allows something like this:
1
2
char array[] = {'a' , 'b' , 'c'};
System.out.println(array);

But when I did it using C++ I get some extra characters after c, that is I did:
1
2
char array[] = {'a' , 'b' , 'c'};
cout<< array;

Any explanations!!!
C-style strings (char arrays) need to be null terminated so that the computer knows where the end of the string is:

1
2
3
char array[] = {'a', 'b', 'c', 0};  // the 0 marks the end of the string

cout << array; // now it works 


Of course, the easier way to write this is as follows:

1
2
3
char array[] = "abc";  // "double quotes" makes the string automatically null terminated

cout << array;
Topic archived. No new replies allowed.