The code your teacher has shown you is an ill-formed program, no diagnostic required, because it violates a requirement that pointers actually point to the thing they claim to be pointed to (otherwise known as "strict aliasing").
As a concrete example, a compiler can analyze your program, notice that A was not directly written to and that no short was written to, and prove that A was never changed once created.
All of that messing around with B can be proven, under the C++ standard, as not being able to modify A in a well formed program.
A for(;;) loop or even a ranged-for is likely to be optimized down to static initialization of A. Your teacher's code, under an optimizing compiler, will optimize to undefined behavior.
If you really need a way to create an array initialized with one value, you could use this:
template<std::size_t...Is>
auto index_over(std::index_sequence<Is...>) {
return [](auto&&f)->decltype(auto) {
return f( std::integral_constant<std::size_t, Is>{}... );
};
}
template<std::size_t N>
auto index_upto(std::integral_constant<std::size_t, N> ={})
{
return index_over( std::make_index_sequence<N>{} );
}
template<class T, std::size_t N, T value>
std::array<T, N> make_filled_array() {
return index_upto<N>()( [](auto...Is)->std::array<T,N>{
return {{ (void(Is),value)... }};
});
}
and now:
int main() {
auto A = make_filled_array<short, 100, 3>();
std::cout << "\n";
print(A.data(),100);
}
creates the filled array at compile time, no loops involved.
Using godbolt you can see that the array's value was computed at compile time, and the value 3 was extracted when I access the 50th element.
This is, however, overkill (and c++14).