1. What does the following do:

void afunction(int *x)
{
x=new int;
*x=12;
}
int main()
{
int v=10;
afunction(&v);
cout<<v;
}
a) Outputs 12
b) Outputs 10
c) Outputs the address of v

Answer Posted / shakti singh khinchi

ANs: b. Output is 10.

bcoz in method afunction() allocates new memory to var x and
change its value after that, but tha actual variable doesn't
changes its location, thats why its remains same as it has
initialised by 10.

But if memory allocation by "new" has not ben done than it
will change the value as 12.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the implicit member functions of class?

585


Why should you learn c++?

565


Explain the difference between class and struct in c++?

580


what is multi-threading in C++?

599


What is string in c++ programming?

604






What is the difference between the indirection operator and the address of oper-ator?

591


Write a program to interchange 2 variables without using the third one.

572


Why is swift so fast?

608


What is prototype for that c string function?

678


Difference between declaration and definition of a variable.

574


Is rust better than c++?

588


Can I learn c++ without c?

589


How is data hiding achieved in c++?

562


Write a Program for dynamically intialize a 2 dimentional array. Eg:5x20, accept strings and check for vowels and display the no.finally free the space allocated .

666


What is the c++ code?

561