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...
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).
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).