2

When declaring a function, what is the difference between the parameters string* and string&? I know that string s would make a copy of the string s and pass it by value, but it seems that on many websites, they refer to string* s and string& s as both passing by reference. I am new to C++ and this is the only thing that i have not been able to figure out myself. Thanks in advance :)

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53

5 Answers5

2

Decent explanation here:

http://www.cprogramming.com/tutorial/references.html

(Hint: there is more to it than "it looks different")

gschandler
  • 3,208
  • 16
  • 16
  • 2
    Add a little context to your link, answers that are just links to external sources are considered *bad* and might be deleted. ([Source](http://stackoverflow.com/faq#deletion)) – Nasreddine Dec 06 '11 at 21:12
  • "Decent explanation" + OP's question = context. – gschandler Dec 06 '11 at 21:14
1

Passing by pointer string* s passes in an address, so s is not of type string, it's an address. If you dereferenced it, that is used some expression including *s, then *s is of string type.

When passing by reference string& s, s is of type string, so no dereferencing is necessary.

tlehman
  • 5,125
  • 2
  • 33
  • 51
  • your description of the reference is wrong. Passing the `string` by reference still passes an address, just like a pointer does. In fact, the compiled machine code for the parameter passing is usually identical whether you use a pointer or a reference. When accessing the passed `string` via a reference, the compiler handles the deferencing for you, whereas accessing the passed `string` via a pointer, you have to do the dereferencing yourself in code. – Remy Lebeau Dec 06 '11 at 21:09
  • Ah, thank you for correcting me. I tried voting my own answer down, but I can't, I'll vote for a better answer and hopefully this one will sink so that it doesn't confuse anyone. – tlehman Dec 06 '11 at 22:09
0

Lots of almost right answers. Many are right for particular implementations but rely on unspecified behavior on the part of a compiler.

Aside from the . vs ->:

  • pointers are first class objects and references are not. You can take the address of a pointer but you cannot take the address of a reference (you'll get the address of the referenced object)
  • references always refer to the same object for the duration of their lifetime. Non-const pointers can change.
  • References cannot be to a "null object". You can write code that does this but you're relying on unspecified compiler behavior
  • You can do arithmetic on pointers

In general, the language spec says very little about references, purposefully giving compiler writers a lot of latitude in implementaiton. The fact that they operate a lot like const pointers is not guaranteed by the spec.

smparkes
  • 13,807
  • 4
  • 36
  • 61
0

I find pointer parameters quite useful in two ways:

  1. The caller has to pass a pointer which could very well be the address of an automatic variable. This makes it obvious to the person reading the calling code that the variable whose address is being passed is likely to change.
  2. Pointers can have a NULL value, which acts like a sentinel. References cannot do that. For example, in your own example of string & versus string *, an empty string could be an acceptable value for the variable and hence useless as a sentinel. However, a NULL value for the pointer parameter tells your function that the string * argument was not set by the caller. The function that receives a string & has no way to determine if the caller has not set the value or is content with the default initialized value.
Happy Green Kid Naps
  • 1,611
  • 11
  • 18
-2

The main difference is how the function will be called. Consider these examples:

void f(string *s)
{}
int main() {
   string s;
   f(&s);
}

and the reference version:

void f(string &s) { }
int main() {
   string s;
   f(s);
}

So the function call looks different...

`

Macmade
  • 52,708
  • 13
  • 106
  • 123
tp1
  • 1,197
  • 10
  • 17
  • additionally, if you use `string *s`, when using s inside f() you'll have to dereference the variable to access your data; with `string &s`, you can use just `s` and the dereferencing is implicit. – Lie Ryan Dec 06 '11 at 21:08
  • No shit, how the function call looks was the original motivation of the references in relation to operator overloading. If that's not significant motivation for references, then I don't know what it it. (especially for functions -- the references in data members is whole completely different ballgame) – tp1 Dec 06 '11 at 21:16