Thread: wondering why output is for a on first print is 2686780

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    14

    wondering why output is for a on first print is 2686780

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int f(int *d, int c, int b, int *a);
    int main(void){
        int a=1, b=3, c=2, d=4, e=5;
        e = f(&a, f(&b,a,e,&c), e, &d);
        printf("a =%d b=%d c=%d d=%d e=%d\n", a, b, c, d, e);
        system("pause");
        return 0;
    }
    int f(int *d, int c, int b, int *a) {
        
        *a = *a + 1;
         c = c + (*a);
         b = b + c;
        *d = *a + 2;
        printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
        return *d - *a + 2;
    }
    This was a test question we had on the test and i believe the real output is
    a=3 b=9 c=4 d=5
    a=5 b=14 c=9 d=7
    a=7 b=5 c=3 d=5 e=4

    I was wondering if anybody could find a reason why when i output it on my computer then i get
    a = 2686780, b = 9 c = 4, d = 2686784
    a = 2686776 b = 14 c = 9 d = 2686788
    a = 7 b = 5 c=3 d=5 e = 4

    bascially why in the function is a and d crazy... i imagine its something to do with the * and & might be messed up...just curious is all.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dl27
    bascially why in the function is a and d crazy... i imagine its something to do with the * and & might be messed up...
    Yeah, in function f, a and d are pointers. Since you want to print what they point to, you should have written:
    Code:
    printf("a = %d, b = %d, c = %d, d = %d\n", *a, b, c, *d);
    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. Wondering output???
    By luckyluke in forum C Programming
    Replies: 4
    Last Post: 07-22-2010, 03:14 PM
  2. Replies: 17
    Last Post: 10-23-2008, 09:10 PM
  3. How do I print my output?
    By Hexadakota in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2008, 04:29 PM
  4. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 09:45 AM

Tags for this Thread