char and int arrays

Nov 5, 2011 at 9:46am
int array1[5]={1,2,3,4};
char array2[5]="cpp";
cout<<array1<<endl;
cout<<array2;

The above program gives me the output as:
memory address of array1[0].. and "cpp"

if i say cout<<array1 and it gives me the memory address of the first element of the array, shouldnt cout<<array2 also give me the memory address of the first element of the character array instead of showing me "cpp"?

basically what i am trying to find out is how are char arrays different from int arrays? and how do they work? and why do they work the way the do?
please help me out guys...
Nov 5, 2011 at 9:58am
First array is int*, the second is a char*. It's natural that cout treats int* and char* differently (For a char* it will print a string it points to, and for any other *, it will print the address).
Nov 7, 2011 at 6:49pm
so it is only cout that treats it differently?
or whenever i write array2 it will evaluate to the value of the entire string?
Nov 7, 2011 at 7:55pm
Both array1 and array2 (when written without []) are pointers. They don't evaluate anything at all. It's only cout that treats them differently. You can easily write functions that treat them differently too (see function overloading).
Topic archived. No new replies allowed.