The computeCube function is correct, but the main function has a problem. Explain why it may not work and show a way to fix it. Your fix must be to the main function only; you must not change computeCube in any way.
void computeCube(int n, int* ncubed)
{
*ncubed = n * n * n;
}
int main()
{
int* ptr;
computeCube(5, ptr);
cout << "Five cubed is " << *ptr << endl;
}
ptr has not been initialized
#include <iostream>
using namespace std;
void computeCube(int n, int* ncubed)
{
*ncubed = n * n * n;
}
int main()
{
int* ptr; //The pointer has been declared but not initialized
ptr = new int();
computeCube(5, ptr);
cout << "Five cubed is " << *ptr << endl;
delete ptr;
return 0;
}
Comments
Leave a comment