wondering why output is for a on first print is 2686780

This is a discussion on wondering why output is for a on first print is 2686780 within the C Programming forums, part of the General Programming Boards category; 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, ...

  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
    21,993
    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);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:29 PM
  4. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 08:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21