Exercise 1 A Puzzle Try compiling the following program: void foo(const int *); int main() { int** V = new int [10]; foo
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Exercise 1 A Puzzle Try compiling the following program: void foo(const int *); int main() { int** V = new int [10]; foo
Exercise 1 A Puzzle Try compiling the following program: void foo(const int *); int main() { int** V = new int [10]; foo(V); return 0; 3 The compiler will give you an error because you are converting intto const inte. The erro message looks like this with gcc 11.2.0; test.cpp: In function 'int main()': test.cpp:5:7: error: invalid conversion from 'int' to 'const int *' [-fpermissive] 51 foo(s); 1 1 int** test.cpp:1:10: note: initializing argument 1 of 'void foo(const int**)' il void foo(const inte); 1 Usually it is possible to convert from non-const to const - why is that not possible here? Hint It is obvious why the following program does not compile: const int* bar(); int main() { int** v = new int [10]; v[O] - bar(); return 0; } What does this program have in common with the program above?
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!