简体   繁体   中英

c++ passing pointers to a function

I am trying to create a console app in C++ that prompts the user to enter a floating point number and then takes that number and separates out the integer part and the fraction part.

Example output would be:-

Please enter a floating point number:
800.589
The integer part is 800 and the fraction part is .589

My solution is shown below:

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double anyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
fractionPart = fmod(anyNumber,1);
integerPart = anyNumber - fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}

int main()
{   
cout << "Please enter a floating point number: ";
double anyNumber = 0;
cin >> anyNumber;
cout << endl;    
spliceAnyNumber(anyNumber);
system("Pause");
return 0;   
}

I wrote the program but I am also being asked to pass pointers to the function and manipulate the dereferenced values. I tried to do that below but I am getting a bunch of errors back from the compiler.

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1);
&integerPart = &anyNumber - &fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " <<     *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}

int main()
{   
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber;
cin >> *pAnyNumber;
cout << endl;    
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;   
}

Where am I going wrong with adding in pointers? Version 1 works but version 2 does not.

I've notated this inline.

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double *pAnyNumber)
{
  double integerPart = 1;
  double fractionPart = 1;
  double *pIntegerPart = &integerPart;
  double *pFractionPart = &fractionPart;
  &fractionPart = fmod(&anyNumber,1);  // <- you should dereference pAnyNumber instead, and assign to fractionPart (i.e. "fractionPart = fmod(*pAnyNymber, 1);
  &integerPart = &anyNumber - &fractionPart;  // <- similar as above
  cout << "The integer part is " << *pIntegerPart << " and the fraction part is " <<     *pFractionPart << "\n"; *pFractionPart << "\n";
  cout << endl;
  cout << "The address of *pIntegerPart is " << &integerPart << "\n";
  cout << endl;
  cout << "The address of *pFractionPart is " << &fractionPart << "\n";
  cout << endl;
}

int main()
{   
  cout << "Please enter a floating point number: ";
  double *pAnyNumber = &anyNumber;  // <- you haven't declared an 'anyNumber' variable to take the address of
  cin >> *pAnyNumber;
  cout << endl;    
  spliceAnyNumber(*pAnyNumber);
  system("Pause");
  return 0;   
}

You have to declare anyNumber before you can dereference it:

double *pAnyNumber = &anyNumber; // references an undeclared variable

Just take the address when passing to the function. Before that, you can use normal variables - no need for pointers:

double anyNumber;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(&anyNumber); 

Additionally, you're using the wrong operator in your function. It should be like this:

*pFractionPart = fmod(*pAnyNumber,1);
*pIntegerPart = *pAnyNumber - fractionPart;

The other thing is invalid syntax: &variable = ... literally means "address of variable = ", which results in a double** .

So the only change you have to make is the function parameter, and accessing it. No need for all those pointers inside the function..

The & operator takes the address of a variable, so typeof(&anyNumber) == double** . You want the * operator instead.

You should read double *pAnyNumber as "When I apply the * operator, I get a double ". (You actually get an lvalue reference, but that doesn't roll off the tongue and will probably confuse you...)

Your main function is a mess; leave it the same as the original and change spliceAnyNumber(pAnyNumber); to spliceAnyNumber(&pAnyNumber); .

I assume when you wrote anyNumber you actually meant pAnyNumber . If you have a pointer

double* p;

You dereference by *p , not &p . The former gives you a double while the latter gives you a double** .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM