Thread: return double issue

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    return double issue

    I have the below code which has issue with double return value. How can I fix it?

    Code:
    double *funct( double i )
    {
       		double result = 2.0 * i;
       		return &result;
    }
    Last edited by leo2008; 12-09-2021 at 07:34 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,435
    Don't return a pointer would be one fix.

    Make the variable static would be another, but then you couldn't call the function multiple times in the same expression.

    Allocate some memory, but then the caller has another problem to deal with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Quote Originally Posted by Salem View Post
    Don't return a pointer would be one fix.

    Make the variable static would be another, but then you couldn't call the function multiple times in the same expression.

    Allocate some memory, but then the caller has another problem to deal with.
    How can I prevent return a pointer in this case?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,435
    Why do you even need to return a pointer in this case?

    Code:
    double funct( double i )
    {
            double result = 2.0 * i;
            return result;
    }
    and all the problems of a pointer go away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by leo2008 View Post
    I have the below code which has issue with double return value. How can I fix it?

    Code:
    double *funct( double i )
    {
               double result = 2.0 * i;
               return &result;
    }
    It's hard to answer because no-one would write a function to muliply a real value by two. It must be placeholder code.

    If you are calculating one value, return it as a "double" (not a double *). If you are calculating several values, but only a small number, then pass in pointers to the values and set them in the function. If you are calculating a long list of values, or if the number of values isn't fixed (e.g. a list of prime factors for an integer), then allocate memory with malloc, fill it with the values, then return it. Usually you will also have to pass in a count varibale via a pointer.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to return a double correctly?
    By Terrance in forum C Programming
    Replies: 2
    Last Post: 10-15-2016, 08:46 AM
  2. To return a double value
    By shruthi in forum C Programming
    Replies: 4
    Last Post: 09-12-2012, 10:52 AM
  3. pow() doesnt return a double?
    By FloatingPoint in forum C++ Programming
    Replies: 23
    Last Post: 08-04-2003, 11:11 AM
  4. Rounding issue with double
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-08-2002, 08:11 PM
  5. return as a double?
    By Dummies102 in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 08:25 PM

Tags for this Thread