(C++) Write a function that creates a new variable on the stack. Give the variable a value of 3. Print out the value and the address of the variable within the function. Return the address of this variable, and make sure that main has a pointer set to the returned variable (int *x = func(); Print out the address of the variable and the value in the variable in main. Did this work? Did it compile? Include a comment on those 2 questions.
#include <iostream> using namespace std; int* func(){ int i = 3; cout<<"In func:"<<endl; cout<<"Value = "<<i<<endl; cout<<"Address = "<<&i<<endl; return &i; } int main(){ int* x = func(); cout<<"\nIn main:"<<endl; cout<<"Value = "<<(*x)<<endl; cout<<"Address = "<<x<<endl; return 0; }
///////////////////////////////////////////// Did this work? No Did it compile? Yes
Get Answers For Free
Most questions answered within 1 hours.