Why does int* ptr_arr_int = {1,2}; gives a compiler error, but whereas char* ptr_arr_char = "amruth"; compiles fine?
int* ptr_arr_int = {1,2}; // ->ERROR
char* ptr_arr_char = "amruth"; // ->OK
"amruth" is a const char[7] type in C++, and a char[7] type in C (although the behaviour on attempting to modify the string is undefined).
This can decay to a const char* or char* type respectively in some circumstances, such as yours.
Although an int[2] will similarly decay to an int* in some circumstances, {1, 2} is neither an int[2] nor a const int[2] type; rather it is a brace-initialiser.
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