purpose of pointers

the one function using pointers, the other just returning. What is the purpose of using pointers here as return simplifies the syntax?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void change(int* var){
    *var += 1;
}

int change2(int var){
    return var += 1;
}

int main(){
    int a = 10;
    int b = 22;
    change(&a);
    b = change2(b);
    cout << a << endl;
    cout << b << endl;
}   
Sometimes you need several output parameters or the return type is different than the type of the output parameter. For example

1
2
3
4
5
bool decrease( int *x, int *y )
{
   --*x; --*y;
   return ( *x == 0 && *y == 0 );
} 


Or a function deals with a character array (or any other array). For example C function strchr

char * strchr( const char *s, char c );
Last edited on
> What is the purpose of using pointers here as return simplifies the syntax?

In general, use pointers only when you have to.

If pass by value is adequate, use that.
If not, consider pass by reference.
If that too is inadequate, pass a pointer by value.
i ended up watching http://www.youtube.com/watch?v=Rxvv9krECNw
which the 4MB pass by reference part made sense
Last edited on
Topic archived. No new replies allowed.