Page 1 of 1

Exercise 1 A Puzzle Try compiling the following program: void foo(const int *); int main() { int** V = new int [10]; foo

Posted: Sat May 14, 2022 4:39 pm
by answerhappygod
Exercise 1 A Puzzle Try Compiling The Following Program Void Foo Const Int Int Main Int V New Int 10 Foo 1
Exercise 1 A Puzzle Try Compiling The Following Program Void Foo Const Int Int Main Int V New Int 10 Foo 1 (48.92 KiB) Viewed 43 times
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?