Expected output with pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gretchen1
    New Member
    • Nov 2007
    • 5

    #1

    Expected output with pointers

    I have to find out what would be the output to the following. I believe the output would be 5656 but I wanted to see if I might have overlooked something.

    int x=5, y=6, *p=&x, *q=&y;
    x=*q;
    *p=*q+2;
    *q=x;
    printf("%d %d %d %d\n", x, y, *p, *q);
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    So, what stops you from actually trying to compile the code and run it? Part of being a programmer is experimenting.. .

    Comment

    • chella
      New Member
      • Mar 2007
      • 51

      #3
      Originally posted by gretchen1
      I have to find out what would be the output to the following. I believe the output would be 5656 but I wanted to see if I might have overlooked something.

      int x=5, y=6, *p=&x, *q=&y;
      x=*q;
      *p=*q+2;
      *q=x;
      printf("%d %d %d %d\n", x, y, *p, *q);
      Hi,

      I executed your code. I got 8 8 8 8 as output. I am so suprised about the declaration you have done for the pointers.

      int x=5, y=6, *p=&x, *q=&y;

      what is the difference between declaring and defining the pointer at the same moment(int *p=&x).

      Can you explain on this?

      Comment

      Working...