Is there any difference between & array::front
and array::data
?
ex 1 :
const char* cstring = "Test String";
array<char, 12> carray;
std::memcpy(&carray.front(), cstring, 12);
ex 2:
const char* cstring = "Test String";
array<char, 12> carray;
std::memcpy(carray.data(), cstring, 12);
are the above two same? Or is there any special usage of array::data
?
The difference is simply that front()
returns a reference to the first element and data()
returns a pointer to it. For zero-sized arrays, the former is undefined and the latter is unspecified. For non-zero-sized arrays, data()
is exactly equivalent to &front()
.
In this case, you should prefer data()
simply because you need a pointer and that gives you a pointer.