-
differnt arrays output~
Hi, all~
I have 2 arrays which contains chars and integers. To my surprise I get different answer when cout them. Here is the code and output below :
Code:
#include <iostream>
void main ()
{
int arr_int[] = {1, 2, 3};
int arr_char[] = "Howdy!";
cout << "arr_int: " << arr_int << endl;
cout << "arr_char: " << arr_char << enl;
}
/*
here is the output :
0x22ff60
Howdy!
for what reason we couldnt get the contents of int array as what char do ? :confused:
-
Arrays
It has to do with the iostream classes, i think..
arr_int is a pointer to the first item of the int array, so if you use *arr_int, you should get the output "1".
You are now just printing the address of the first element.
When you're getting a readable output from sending the address of the first char array item, this has to be the way iostream handles chars. The logical thing to do in this case, is to print the array until first space or \0
-
I think only char arrays are NULL terminated automatically, which is why you can "cout" the whole array, this is what makes them special.
-
>cout << "arr_int: " << arr_int << endl;
arr_int is an array of integers, cout has no special instructions to print the array as a whole, so it assumes you are trying to print the address of the first item, which is what you get. If you wanted the contents of the first item you would use this:
cout << "arr_int: " << *arr_int << endl;
>cout << "arr_char: " << arr_char << enl;
Arrays of char are treated differently because cout will assume that it is a nul terminated string of characters and print every item up to the nul character. If you want the address of the first item or the contents of the first item, you have to say so explicitly:
cout << "arr_char: " << &arr_char << endl;
or
cout << "arr_char: " << *arr_char << endl;
Also, your example program has quite a few errors:
>void main ()
You've been told this before, several times. void main is undefined, do not use it.
>int arr_char[] = "Howdy!";
This needs to be an array of char for the initialization to a string literal to work.
>cout << "arr_int: " << arr_int << endl;
Unless you are using a weird compiler, you need to either place some using statements or prefix cout and endl with std:: to account for namespaces.
>cout << "arr_char: " << arr_char << enl;
endl is spelled wrong. ;)
-Prelude
-
Ok, I'm splitting hairs here, but I'm pretty sure the most recent Bloodshed IDE, which is built on the GNU compiler, still allows use of standard headers without explicitly naming a namespace. Apparently, if no namespace is listed, the compiler defaults to namespace std, just like it will add default constructors, etc., under the appropriate circumstances. Also, many older compilers still in use aren't namespace compliant and are still good (not top of the line, and certainly not up to date, but still good) compilers, unless you are the type who has to have the latest and greatest with everything you use. Should you explicitly use namespaces if your compiler is compliant, yes, just as you should implement your own default constructor. But is the compiler weird if it lets you get by without it? IMO, not necessarily.
The only array that can be handled by the << operator as a whole entity is a null terminated character array, that is, a string. You could wrap the array in a class and then overload the << operator for the class so << will have that type of functionality if you really wanted to. But that's probably a little further down the learning curve.
-
If you don't mind using unnecessary non-standard features then I'm not stopping you, but I use and encourage standard C++ wherever possible because platform portability is one of C++'s greatest strengths.
-Prelude