Reputation: 431
i don't understand how the pointer can assign a char array or a string as :
char* Carr = new char[5];
Carr = "Hello";
however this below code doesn't work :
int* Iarr = new int[5];
Iarr = { 1,2,3,4 };
I know that the pointer can assignment a reference not a value, so how the compiler accept the string?
Upvotes: 0
Views: 2699
Reputation: 20244
When you use
Carr = "Hello";
after allocating the char
pointer using new
, you lose the reference to the allocated memory (leading to a memory leak) and you make Carr
point to a string literal "Hello"
.
Here,
Iarr = { 1,2,3,4 };
you assign an int*
to an initializer list (which doesn't make any sense). This is why the compiler complains when you attempt to do this.
Upvotes: 1
Reputation: 726579
String literals (i.e. sequences of characters enclosed in double quotes) are a special case. C++ compiler has special code that places their content in a designated area inside the code, and hands your program a pointer to it. String literals also happen to be compatible with const
character pointers, so you can make an assignment (you should get a warning or an error, because your Carr
is not const
).
There is no such handling for arrays of integers. Although you can provide initializers for them, you cannot make aggregates of them in the same way that you make "aggregates of characters" by placing them in double quotes: the compiler does not provide support for it.
Fortunately, most of the time you do not have to do any of that, because C++ standard library provides superior alternatives for arrays and strings. For example, you can rewrite your Iarr
initialization like this:
auto Iarr = vector<int> {1, 2, 3, 4, 5};
Upvotes: 1
Reputation: 145279
Neither the first nor the second code snippet is valid in C++ (after 2011).
Before the C++11 standard C++ supported, but had as deprecated since the first C++ standard in 1998, an implicit conversion of string literal to pointer to non-const
item, yielding a pointer to the first character.
Since that's pretty dangerous it was removed.
A corrected version of your first example:
char const* Carr = new char[5];
Carr = "Hello";
Here the assignment to Carr
does not assign an array, but a pointer to the first item in the array.
It invokes an implicit conversion from array to pointer to first item, called a type decay. There is a corresponding decay (implicit conversion) for function to function pointer.
Compilers may still, as a compatibility feature, or just because they've not been fixed, support the conversion from string literal to pointer to non-const
. You may be able to find some option to turn that off for your specific compiler, or at least yield a warning.
Upvotes: 0
Reputation: 254461
The first one doesn't assign to the array. It changes the pointer, so that it points to the static array defined by the string literal instead of the dynamic array you allocated. The dynamic array is now leaked, with no way to access it. In modern C++, this shouldn't compile, since the literal is constant, so only a const char *
can point to it.
The second one doesn't work because, unlike a string literal, a braced list doesn't create an array or anything else that you can assign to a pointer.
If you want an assignable array, use std::array
, std::string
or std::vector
.
Upvotes: 6