If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Explain the code please,how it outputs 2.

Let's start with why you think it should (or shouldn't) output 2. Then when we see your explanation, we will correct it if it isn't correct.

Otherwise, where do we start explaining to you C++? Do we start with what main() is? What a variable is? What pointers are? What a function is? etc. etc. We have no idea what your level of knowledge of C++ is, so it makes no sense for an explanation without first seeing how much you know.

Re: Explain the code please,how it outputs 2.

The other questions is: Why write code like this? What's the purpose? It looks like 'c' code for returning values as part of a function argument without using pass by reference. This isn't good c++ code - its extremely difficult to understand what's going on. Also, you have a memory leak. You are obtaining memory in function f but never releasing it.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Explain the code please,how it outputs 2.

What was the question given to you? It couldn't have just been "why does this output 2". If that was the question, then the answer would have been

"It outputs 2 because that is what you coded it to do".

If it was "what is the final output?", then yes, you need to know what passing a pointer to a pointer is supposed to achieve and simple recursion (plus the basic C++ stuff such as function calling, etc.)

But otherwise, asking these types of questions many times isn't a test of how much you know. Even if you knew what a pointer to a pointer and what recursion does, a simple slip-up with questions that have moderate to difficult syntax could yield a wrong answer (because we are humans, not computers). Granted the question isn't that difficult, but I would be careful in giving these "run the program using pencil and paper" or "run the computer in your head" questions, unless the code is simple enough to debug by hand.

Re: Explain the code please,how it outputs 2.

Having a question like this as part of a written exam IMO serves no really useful purpose and doesn't do much for evaluating pupils understanding. It would be better if this type of question was asked in a practical where there is a logical error that the pupil has to find.

In a written exam to test say function parameters, IMO a better question would be along the lines of
'Describe with examples the different ways of returning values from a function highlighting the pros and cons of each method'

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Explain the code please,how it outputs 2.

It would be better if this type of question was asked in a practical where there is a logical error that the pupil has to find.

Sort of like the questions that Gimpel software would ask when they are promoting their PC-Lint product.

They would ask "The programmer expected x, but y happened. Can you spot the error?". The code was simple enough, so the questions were answerable (the usual answer was that some aspect of C++ was ignored or assumed).

But other than that, these questions with winding logic full of loops, if statements, recursion, and difficult syntax asking "what is the output" can have an experienced programmer give the wrong answer. Not because the programmer is not smart, but because a human can easily make a misstep when attempting to mimic the computer.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.