How to use reference pointers or pointers

closed account (4ET0pfjN)
Hi, I need help with this generic question on ref ptrs and ptrs, I don't understand what to do.

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.

I am trying to learn about pointers and I think I get it, but no clue what to do with this question.
I'll give you a hint. The function needs to take a third argument which is either a pointer or reference, which the caller is responsible for initializing. Pointers and references can be used to tell a piece of code where to put a piece of data.
Last edited on
The following code shows how to get the results when using pointer/reference
1
2
3
4
5
6
7
8
9
void Add(int *result, int a, int b) // Getting the result with a pointer
{
  (*result) = a + b; // Note: pointer needs to be dereferenced (again *) in order to set the content
}

void Add(int &result, int a, int b) // Getting the result with a reference
{
  result = a + b; // Note: no dereference required
}
closed account (4ET0pfjN)
Hi coder777, how would I call your function and shouldn't it return an int
so it should be int Add(int *result, int a ,int b)?

a pointer can only refer to one memory address, so how do i call your function?
Topic archived. No new replies allowed.