Thread: call-by-reference wrong output

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    3

    Question call-by-reference wrong output

    Hi,

    i hope someone can help me... i coded the this function:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    struct parabel {
        double a;
        double b;
        double c;
    };
    
    
    int scheitelhoehe(struct parabel p, double *y) {
        int tmp;
        int a1 = 0, b1 = 0, c1 = 0;
        a1 = 1; 
        b1 = 2; 
        c1 = 3;
        tmp = 0;
    
    
        if (a1 != 0)
        {
            tmp = -(b1 / (2 * a1));
            *y = a1*pow(tmp, 2) + (b1*tmp) + c1;
            return 0;
        }
        else
        {
            return 1;
        }
    }
    
    int main() {
        struct parabel p[] = {
                { 1, 2, 3 },
        };
        double y = 0;
        scheitelhoehe(p[0], &y);
        printf("y = %d\n", y);
    }
    can somebody tell me why the output is 'y = 0' and not 'y = 2'?
    thank you sooooo much!

    kongkong

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since y is a double, you should use %f rather than %d with printf:
    Code:
    printf("y = %f\n", y);
    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
    Jun 2015
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Since y is a double, you should use %f rather than %d with printf:
    Code:
    printf("y = %f\n", y);
    Thank you very much, shame on me for not seeing that obvious little bastard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-11-2013, 06:53 PM
  2. Replies: 4
    Last Post: 02-10-2011, 02:50 PM
  3. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  4. help!! call-by-reference and call-by-value
    By geoffr0 in forum C Programming
    Replies: 14
    Last Post: 04-01-2009, 07:15 PM
  5. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 09:06 AM