Quick Question regarding Pointers

ok.. quick summary of what i know:
1
2
3
4
5
char * var;
// var is a pointer variable that holds the memory address where the value is stored

var = &letter;
// the & allows us to assign the memory location of letter rather than it's value 


So when I want to pass the refference of an object to a function, which of the following is correct?
1
2
3
4
void function(char *var) {...}

char letter = 'a';
function(letter);

or
1
2
3
4
void function(char var) {...}

char letter='a';
function(&letter);

or (as i've seen in some code)
1
2
3
4
void function(char &var) {...}

char letter='a';
function(letter);


It can be rather confusing
ok.. i just tried different ones till i got rid of the compiler error.

It would seem that the third method is the correct method.
It depends on how you want to use it and what scope you want for the variable

1
2
3
4
void func(char *var) {...}

char letter = 'a';
function(&letter)


1
2
3
4
void func(char var) {...}

char letter = 'a';
function(letter)


1
2
3
4
void func(char &var) {...}

char letter = 'a';
function(letter)


All of those should work. However, if you modify letter during func on the second one, it won't modify the original. Whereas the other 2 will.
Topic archived. No new replies allowed.