Help decipher C++ Code Fragment

I came across the following C++ code snippet on the Internet and have been struggling to decipher what it displays. So far, I have determined that some of the numbers it displays are 5 and 8. Can someone please help me with the rest; I don't think it displays more than 4 or 5 numbers?

--------------------------------------C++ CODE FRAGMENT----------------------------------------

      struct A { int st; int d; int r; };
      int main (void)
      A a={5, 5, 5}, b={4, 4, 4}, *c=&a;
      c->st =99; a.d += b.r; int & m = c->d;
      cout <<;
      cout << a.d--;
      cout << c->r;
      cout << m;
      cout << (b.d + 4);
            return 0;

Who is Participating?
jkrConnect With a Mentor Commented:
Just compile and run it:


This seems to be some HW piece about teaching operator precedence and indirection. Hint:

     c->st =99; // changes, since c=&a;
     a.d += b.r; // 5+5=9
     int & m = c->d; // m=5

     cout <<; // decrements *before* output
     cout << a.d--; // decrements *after* output
     cout << c->r; // is actually a.r
     cout << m; // as above
     cout << (b.d + 4); // b.d has never been changed
AxterConnect With a Mentor Commented:
Hi computerese,
> #include<iostream.h>

iostream.h is not part of the C++ standard, and should not be used in any new code.

You should use <iostream> instead, which will compile on modern compilers like VC++ 7.x and 8.x.
You'll probably want to use "using namespace std;" if you don't prefix objects with std::

David Maisonave (Axter)
computereseAuthor Commented:
@ jkr & Axter:

Thanks for your timely replies. This was something I found on a website so I don't know how it was originally used. I was trying to understand it without compiling it; I was aware that I could have compiled it and it would have been interesting to see the results. But your explanations provided the "insight" into what was happening, which is what I really needed most.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.