Reputation: 31
I already searched, and couldn't find anything exactly like this one. Although there do seem to be similarities. If a mod or admin thinks this is a duplicate topic, please let me know which one and ask me how I think it might be different before you delete it.
If I'm returning an int value in a function, how do I return a second one indirectly with a pointer or reference parameter? I have a practice problem I need to do that is asking for this:
Write a function that takes two input arguments and provides two separate results to the caller, one that is the result of multiplying the two arguments, the other the result of adding them. Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.
And I also need to return a string pointer through a function.
- Write a function that prompts the user to enter his or her first name and last name, as two separate values. This function should return both values to the caller via additional pointer (or reference) parameters that are passed to the function. Try doing this first with pointers and then with references.
I already did this one with a reference to a pointer, but I'm having trouble doing it with pointers.
#include <iostream>
using namespace std;
string userName(string user_first_name, string user_last_name);
int main()
{
string user_first_name;
string user_last_name;
cout << "Program for taking in user's full name\n";
cout << userName(user_first_name, user_last_name);
}
string userName(string user_first_name, string user_last_name)
{
cout << "Please provide your first name: ";
cin >> user_first_name;
cout << "Please provide your last name: ";
cin >> user_last_name;
string full_name = user_first_name + " " + user_last_name;
return full_name;
}
Whenever I try to use those string variables as pointers, I get an error saying I can't convert a string to a string*, or I get an error saying I can't convert a string* to a string**.
As for the other one, this is the code I have:
#include <iostream>
using namespace std;
int* pointerMath();
int main()
{
cout << "Let's do some math!\n";
pointerMath();
}
int* pointerMath()
{
int value1;
int value2;
cout << "Provide the first number: ";
cin >> value1;
cout << "Provide the second number: ";
cin >> value2;
int *multiplication_result = value1 * value2;
int *addition_result = value1 + value2;
cout << "The numbers added together are: " << *addition_result << '\n';
cout << "The numbers multiplied by each other are: " << *multiplication_result << '\n';
return multiplication_result;
}
Whenever I run this, it always crashes right after getting both input values, and I get an error code 255.
Edit:
I also have to further modify the string name program, per this question's specification:
- Modify the program you wrote for exercise 1 so that instead of always prompting the user for a last name, it does so only if the caller passes in a NULL pointer for the last name.
How do I do this?
Upvotes: 1
Views: 2383
Reputation: 27548
Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.
This is extremely misleading. It's true that you can technically only return one value, but nothing stops you from returning a custom type that internally holds two values. This approach is usually superior to references or out pointers and should be the default choice:
struct Value
{
int x;
int y;
};
Value GetValue()
{
return { 1, 2 };
}
I already did this one with a reference to a pointer, but I'm having trouble doing it with pointers.
You should not do it with a pointer in the first place. Not with a pointer directly and not with a reference to a pointer. A reference would suffice.
string userName(string user_first_name, string user_last_name);
These string parameters are copies. The function will operate on copies of the arguments, and the original strings on the outside will be left untouched.
Whenever I try to use those string variables as pointers, I get an error saying I can't convert a string to a string*, or I get an error saying I can't convert a string* to a string**.
Because a pointer to something is not implicitly convertible to something, and vice versa (this is also true if "something" is itself a pointer).
Just use references:
string userName(string& user_first_name, string& user_last_name);
int *multiplication_result = value1 * value2;
The result of value1 * value2
is an int
. You cannot just take an int
and use it as if it was a pointer. Your code should not even compile but fail with an error message like error C2440: 'initializing' : cannot convert from 'int' to 'int *'
, unless you are using an ancient compiler or invoke it with the wrong settings.
Upvotes: 1
Reputation: 7467
Write a function that takes two input arguments and provides two separate results to the caller, one that is the result of multiplying the two arguments, the other the result of adding them. Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.
int multiplyAndAdd(int a, int b, int& sum){
sum = a+b;
return a*b;
}
calling this is like this :
int sum=0;
int multiply = multiplyAndAdd(2,3,sum);
I am in favor of using a reference instead of a pointer. In the latter case it would be like this :
int multiplyAndAdd(int a, int b, int* sum){
*sum = a+b; //--> probably a null check is needed
return a*b;
}
and then calling like this :
int sum=0;
int multiply = multiplyAndAdd(2,3,&sum);
the string exercise is almost the same: with references:
void returnstrings(string& s1, string& s2){
//whatever here you just assign s1 and s2
}
calling it :
string firstName;
string lastName;
returnstrings(firstName,lastName);
with pointers :
void returnstrings(string* s1, string* s2){
//whatever here you just assign *s1 and *s2
}
calling it :
string firstName;
string lastName;
returnstrings(&firstName,&lastName);
Upvotes: 0
Reputation: 363
For the first problem, you need use &
to pass your string value. I think it's better to understand than using passing pointer.
also you can pass the pointer like this:
#include
using namespace std;
string *userName(string user_first_name, string user_last_name);
int main()
{
string user_first_name;
string user_last_name;
cout << "Program for taking in user's full name\n";
cout << *(userName(user_first_name, user_last_name));
}
string* userName(string user_first_name, string user_last_name)
{
cout << "Please provide your first name: ";
cin >> user_first_name;
cout << "Please provide your last name: ";
cin >> user_last_name;
string *full_name = new string;
*full_name = user_first_name + " " + user_last_name;
return full_name;
}
int *multiplication_result is a pointer, it need to be bind with a value. after that you can make it point to a int value. you can pass the value by this way:
#include <iostream>
using namespace std;
int* pointerMath();
int main()
{
cout << "Let's do some math!\n";
cout<<*(pointerMath()) <<endl;
}
int* pointerMath()
{
int value1;
int value2;
cout << "Provide the first number: ";
cin >> value1;
cout << "Provide the second number: ";
cin >> value2;
int *multiplication_result = new int;
*multiplication_result = value1 * value2;
int addition_result = value1 + value2;
cout << "The numbers added together are: " << addition_result << '\n';
cout << "The numbers multiplied by each other are: " << *multiplication_result << '\n';
return multiplication_result;
}
Upvotes: 0
Reputation: 43044
To do it with pointers, you have to introduce pointers, i.e.
Change this:
string userName(string user_first_name, string user_last_name);
to that (note string*
for pointer parameters):
string userName(string* user_first_name, string* user_last_name);
Moreover, when you sue cin
you have to dereference the pointer, using the (*pointer)
syntax, e.g.:
string userName(string* user_first_name, string* user_last_name)
{
cout << "Please provide your first name: ";
cin >> (*user_first_name); // <-- dereference pointer
cout << "Please provide your last name: ";
cin >> (*user_last_name); // <-- dereference pointer
string full_name = (*user_first_name) + " " + (*user_last_name);
return full_name;
}
And in main()
, you should pass the address of the variables using the address-of &
operator:
int main()
{
string user_first_name;
string user_last_name;
cout << "Program for taking in user's full name\n";
// Use & to get address of (pointers) to first and last name
cout << userName(&user_first_name, &user_last_name);
}
Now that I showed you some sample code, you should be able to use a similar coding pattern for the other case.
Upvotes: 1