c++ - assigning const array pointer to non const array pointer -
#include <iostream> int main() { const int l_loc[3] = {1,2,3}; int (*l_loc2)[3] = null; l_loc2 = const_cast<int (*)[3]>(l_loc); // complaining here. std::cout<<l_loc2[1]<<std::endl; return 0; }
./example.cpp:7: error: invalid const_cast type ‘const int*’ type ‘int (*)[3]’
i can modify pointed line. clue please.
#include <iostream> int main() { const int l_loc[3] = {1,2,3}; int (*l_loc2)[3] = null; l_loc2 = const_cast<int (*)[3]>(l_loc); // complaining here. std::cout<<l_loc2[1]<<std::endl; return 0; }
l_loc
pointer first integer in array. address contains constant. can't reassign l_loc
point array of integer constants. why must cast address of l_loc
. instead of l_loc2 = const_cast<int (*)[3]>(l_loc)
need pass address of l_loc
i.e. &l_loc
. result l_loc2 = const_cast<int (*)[3]>(&l_loc)
hope helps.
Comments
Post a Comment