Thread: Output of code

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Output of code

    Can anyone tell me what the output of this code is suppose to be?
    It is from a test exam

    Code:
     int tal2= 15;
    int tal3= 17;
    int * p = &tal2;
    int * q = &tal3;
    cout <<tal2 <<" " << *q <<endl;
    (*p)++;
    cout <<*p<<" " << tal3 <<endl;
    p = p + 1;
    cout <<*p<<" " << *q << endl;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write a program such that you can execute the code and observe its output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    9
    i can't do it from my computer. I just thought that it would be very easy to answer the question.

    Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lesodk
    i can't do it from my computer.
    Why not? There are C++ compilers available for download and use at zero price.

    Quote Originally Posted by lesodk
    I just thought that it would be very easy to answer the question.
    Yes, it is. Why don't you give it a try? I think that the point is to explain why the output is what it is, rather just saying what is the output.

    EDIT:
    Okay, maybe I spoke too soon. Looking more carefully at the code, there is one part is actually results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    9
    Well, i would think that p = p + 1makes the pointer invalid, this the output
    -----
    tal2 17
    16 tal3
    ??? 17
    ------
    here my ?? means, that i don't know what comes there.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lesodk
    i would think that p = p + 1makes the pointer invalid
    That is correct, which is why the latter *p results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM