-4

I made a very basic program to try to better understand pointers but I cannot figure them out. I continue to get the error 'void celsius(double *)': cannot convert argument 1 from 'double' to 'double *'

#include <iostream>

using namespace std;

void celsius(double*);

int main() {

    double temp;
    double value;

    cout << "Enter temperature in C ";
    cin >> value;

    temp = celsius(value);

    cout << temp << endl;

    return 0;
}

void celsius(double *par) {
double tem;

tem = (1.8 * par + 32);
    return tem;
}

After referring to several resources I just cant figure out why it's returning the error. I know the error says it's because it can't convert a double to double* but examples I see online don't explain why.

Any help with the code or explanation be great. Thanks.

CD'A
  • 41
  • 5
  • 1
    You umust take the address of `value` to pass it into a function expecting a pointer. Eg: `temp = celsius(&value);` Note: this will reveal other problems. – user4581301 Jul 13 '18 at 21:41
  • 1
    Just change `void celsius(double*)` to `double celsius(double)`. There is no reason the function needs a pointer. – NathanOliver Jul 13 '18 at 21:41
  • 1
    Also, it sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jul 13 '18 at 21:41
  • `void celsuis(...` also should be changed to `double celsius(...` as you are returning a `double` – kmdreko Jul 13 '18 at 21:41
  • 1
    I recommend exploring *references* before pointers. For example, declare `void celsius(double& variable)`. When passing by reference, the function knows that the parameter exists. When passing a pointer, the point may point to anywhere, including not the variable from the caller. Much safer to use references. – Thomas Matthews Jul 13 '18 at 21:47

2 Answers2

3

You need to make the following changes to your program.

  1. Change the return type of the function to double.

    double celsius(double*);
    
  2. When you call the function, make sure you pass a pointer to a double, not a double.

    temp = celsius(&value);  // Use &value, not value
    
  3. In the function, you need to use *par to dereference the pointer and use its value.

    tem = (1.8 * (*par) + 32);  // Use *par not just par
    

Another option is to leave the return type as is but change the implementation of the function.

void celsius(double *par)
{
   *par = (1.8 * (*par) + 32);
}

and change the usage to just

celsius(&value);
cout << value << endl;
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Why not just suggest changing `void celsius(double*)` to `double celsius(double)`? It is one change instead of 3. – NathanOliver Jul 13 '18 at 21:48
  • @NathanOliver, I did not want to change the OP's motivation - *try to better understand pointers* – R Sahu Jul 13 '18 at 21:49
  • Ah, in that case `void celsius(double *par) { *par = (1.8 * (*par) + 32) }` and `celsius(value); cout << value << endl;` would showcase that more. – NathanOliver Jul 13 '18 at 21:50
  • @NathanOliver, that's also an option too. Thanks :) – R Sahu Jul 13 '18 at 21:53
  • 1
    Appreciate the help! This helps me understand it a lot more. Part of the issue was looking at so many different examples and not seeing what pieced together. But these examples really demonstrate it well :) – CD'A Jul 13 '18 at 22:23
0

I’m sure this has been said a billion times but think of the pointer variable as just a variable.

double* is simply a type and on 32 not machines it can hold 4 Byte values. On 64 not machines there is a bigger addressing space so pointers are bigger.

Then, if double* is just a variable that can store a 4 byte address of a double then it needs the address of something...

You can take the address of other variables using ‘&’

Example:

double* ptr = &anotherVariable;

Now ‘ptr’ contains a 4 byte address and then you can access the variable contents by dereferencing the double pointer itself.

Example:

*ptr = 4.2;

However, the ‘ptr’ variable itself contains an address so the ‘anotherVariable’ that was assigned to it earlier also has the value 4.2

Now, if you were passing a value to a function and the functions parameter was ‘double* temp’. You could pass it the address of a variable or an actual pointer and inside the function you can do all the same things as above.

Bayleef
  • 185
  • 2
  • 10
  • Thanks for this perspective on how these work, this is very helpful with my understanding. Really appreciate. – CD'A Jul 13 '18 at 22:24