Menu

problem with code

D T
2008-02-17
2012-09-26
  • D T

    D T - 2008-02-17

    I have a really simple code

    include <iostream>

    using namespace std;
    int main()
    {

    int m = 44;
    int p = &m;
    int &r = m;
    int n = (
    p)++;
    int q = p -1;
    r =
    (--p) + 1;
    ++ *q;
    cout << m;
    system("pause");
    }

    and i get the output

    2291613

    Can someone please explain why i get this output?

     
    • D T

      D T - 2008-02-17

      thanks for the help

       
    • Anonymous

      Anonymous - 2008-02-17

      Perhaps I'm wrong but that seem home work at all.

      int m = 44; // a integer initiated to 44
      int p = &m; // pointer-to-int initiated with the address of m (p point-to-m)
      int &r = m; // a reference (alias) to m
      int n = (
      p)++; // a int inititated to unary increment of the deference of p = m++ = 45
      int q = p -1; / a pointer-to-int initiated with...
      Here is the trickery. here you decrease the value of the p pointer
      and q points to some place undefined in your code. From this point
      your code is trash.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.