Thread: Arguments return method is not working for float

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    Arguments return method is not working for float

    Below is the source code
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main(void);
    float * Display(float *,float *);
    
    int main(void)
    {
       float a,b,*c;
       clrscr();
    
         printf("Enter two real numbers\n");
         scanf("%f%f",&a,&b);
    
         c=Display(&a,&b);
    
         printf("The addition is %f",*c);
    
       getch();
       return 0;
    }
    
    float * Display(float *a,float *b)
    {
       float c;
       c=*a+*b;
       return(&c);
    }

    The above progarm is not giving proper output while executing.

    Output:
    Enter two real numbers
    2.3
    3.5
    The addition is -NAN

    I am using Turbo C++ Compiler v3.0

    Plz let me know how exactly the code is.

    Thanks

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    it's because c is local variable and it will be destroyed as soon as the function returns.
    • return it by value (it won't hurt ), or
    • allocate (malloc()) and return

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You've not allowed to return the address of a local variable.
    Just don't declare the return type, or c in main, as a pointer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM