Reputation: 1
const char* str = &text; // that's how I was taught.
Why do we initialize const char* with a string and not a ref?
How is it that we initialize the pointer with a string of the "string" type and not with a & ref?
const char* str = "text"; // After all, should the pointer type be initialized with a reference or not?
Upvotes: 0
Views: 187
Reputation: 238381
How is it that we initialize the pointer with a string
A string literal is an array of characters. All arrays will implicitly convert to a pointer to first element of the array. Thus, an array of characters will implicitly convert to a pointer to a character.
Upvotes: 1