Click here to Skip to main content
15,885,711 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I don't understand what **<variable name is doing>
Also what does statement "q=&t" means

C#
#include<iostream.h>
#include<conio.h>

void main()
    {
        clrscr();
        int x[]={10,20,30,40,50};
        int *p,**q,*t;
        p=x;
        t=x+1;
        q=&t;
        cout <<*p<<","<<**q<<","<<*t++;
        getch();
    }


What I have tried:

The output of the programme given by turbo C4 and codeblocks 13.12 is 10,30,20 I tried to understand and according to me the output should be 10,20,20.
Posted
Updated 13-Feb-16 23:20pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Feb-16 0:57am    
Why would you need any explanation of pointer to pointer. *T means "pointer to an object of type T". If T is also a pointer, say, a pointer to an object of type D, what difference can it make. *D is another type, no different from T. So a pointer to *D is *(*D), that is, **D.

To see what your code does, use the debugger.

—SA
THE_Achilles 14-Feb-16 5:10am    
Thanks, can you please give me a link to learn debugging. I had started learning c++ in January itself. So i have no idea of what debugging is.
Sergey Alexandrovich Kryukov 14-Feb-16 14:30pm    
Why a link? What IDE are you using? Use your ID menu and see what it does, refer to the ID's documentation. I never met a person who learned debugging by any manuals. IDE (or stand-along) debugger help does the trick. If you are unsure what some terms mean, ask use about it. Say, to understand what "Call stack" does, you need to understand how call/return mechanism works in general, and the role of the stack. To understand registers (which you rarely access directly with C++), you need to understand how CPU works, and so on...

Anyway, if you are programming without using the debugger, you should stop doing what you are doing and start using the debugger. You need to do it before asking such questions, or should you have any concerns of your runtime, anything which you cannon figure out from the first glance. Why wasting time on walking blindfolded? Only for some advanced training, but not in everyday practice.

—SA
THE_Achilles 14-Feb-16 23:27pm    
I am using Turbo C4 and Codeblocks 13.12 with MinGW.
Sergey Alexandrovich Kryukov 14-Feb-16 23:49pm    
Borland IDE had excellent debugger, not sure about your particular settings. No matter. Just use the debugger you have and its documentation.
—SA

As ppolymorphe pointed out, that is a subtlety of the C programming language ('inherited' by C++ :-) ).
If you break the offending line
Quote:
cout <<*p<<","<<**q<<","<<*t++;

in two ones
C++
cout << *p << "," << **q << ",";
cout << *t++;

then you get the output you expect.
That happens because the order of evaluation of the items feeding cout is not predictable (see, for instance Order of evaluation - cppreference.com[^]).
 
Share this answer
 
Comments
Patrice T 14-Feb-16 5:01am    
My 5 too
THE_Achilles 14-Feb-16 5:13am    
Hehe.. i didn't knew that magic .. wow thanks.. :)
CPallini 14-Feb-16 5:21am    
It is tricky, indeed.
You are welcome
CPallini 14-Feb-16 6:03am    
I would stop using turbo C, it is an obsolete compiler, you are writing non-standard C++ code. There are better free alternatives (for instance Visual Studio Community Edition) or g++ (see http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/ for installing g++ on Windows).
Debugging with Visual Studio is very very simple, you don't even need a tutorial. Debugging with gdb is simple too, just read a tutorial.
nv3 14-Feb-16 5:47am    
Well, I think that is not entirely correct. The order of the evaluation of the << operators is dictated by the left associativity of the << operator. So, first

cout << *p

must be evaluated, then

... << ","

and so forth. Anything else would not make sense, because then the output sequence would be unpredictable.

The clue here is that the arguments of the << operators are expressions themselves and the compiler is obviously free to evaluate these expression in any order before feeding them into their respective << operator. A very subtle point indeed, which I would not have expected.

The lesson to be learned: Never put anything with a side effect into a cout chain!
The program is doing exactly what it is supposed to.
Your problem is learning all subtleties of C syntax.

If you don't understand something, learn the debugger and use it to see exactly what the code is doing.

Google with "Turbo C++ debug" gives 60 millions answers, you should find happiness there.
either in IDE or as standalone app.
Borland Turbo Debugger - Wikipedia, the free encyclopedia[^]
 
Share this answer
 
v2
Comments
CPallini 14-Feb-16 4:56am    
5.
Patrice T 14-Feb-16 5:00am    
Thank you
Patrice T 14-Feb-16 5:52am    
Learning to use Google is also a good skill to have. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900