?
Solved

Help decipher C++ Code Fragment

Posted on 2006-11-29
3
Medium Priority
?
569 Views
Last Modified: 2012-06-22
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?

Thanks.
--------------------------------------C++ CODE FRAGMENT----------------------------------------

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
      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 <<--a.st;
      cout << a.d--;
      cout << c->r;
      cout << m;
      cout << (b.d + 4);
            return 0;
            }

0
Comment
Question by:computerese
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1400 total points
ID: 18037985
Just compile and run it:

989588

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

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

     cout <<--a.st; // 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
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 600 total points
ID: 18038216
Hi computerese,
> #include<iostream.h>

FYI:
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)
Cheers!
0
 

Author Comment

by:computerese
ID: 18039231
@ 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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question