void test3(char * &p){
strcpy( p, "aaaaaaaaaaaaaaaaaaaaaaaaaa");
}
char c[] = "123";
test3(c);
the code above is compiled failed:
initialization of non-const reference of type 'char*&' from a temporary of type 'char*'
why char c[]
can't be referenced by the argument p
?
Because the type of c
is char[4]
, i.e. an aray of four char
s. Your reference needs a char*
, i.e. a pointer to char
.
Arrays are not pointers. In most cases, they decay to a pointer to first element when used, but that decay-produced pointer is temporary. As such, it cannot bind to a non-const reference.
Why is your function taking a reference in the first place? It would be perfectly fine taking char*
.
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