Here is a small code in c++ where I am creating two arrays of char and int data types respectively.However the same print operation is behaving differently for both the arrays
#include<iostream>
using namespace std;
int main()
{
char a[5]={'h','e','l','l','o'};
int b[5]={1,2,3,4,5};
cout<<a; //displays the string "hello"
cout<<"\n"<<b; //displays the address of b[0]
return(0);
}
I expected the output to be the address of the first element of both the arrays i.e. address of a[0] and b[0] respectively however char type array is behaving differently in this case.
It's a special overload of operator << for cout
that treats char *
arguments as null terminated strings and prints the entire string.
If you want to print the address cast it to void *
.
cout << (void *) a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With