7

What does '&' mean in C++?

As within the function

void Read_wav::read_wav(const string &filename)
{

}

And what is its equivalent in C?

If I want to transform the above C++ function into a C function, how would I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
  • 1
    http://en.wikipedia.org/wiki/Reference_(C%2B%2B) – Tim M. Feb 09 '11 at 14:32
  • 1
    as @Tim Medora said http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29 (but I 've corrected the link ;)) – Hernán Eche Feb 09 '11 at 14:35
  • @Hernán Eche - Paste fail on my part...Thanks! – Tim M. Feb 09 '11 at 14:36
  • 4
    "If I want to transform the above C++ function into a C function, how would I do it?" 1. Stop using classes. 2. Stop using strings. 3. Stop using pass by reference. – David Heffernan Feb 09 '11 at 14:38
  • While the answers apply _to this situation_, keep in mind that the [ampersand (&) can mean other things](https://dev.to/sandordargo/how-to-use-ampersands-in-c-3kga) in C++ as well. – quant Jan 02 '21 at 04:51

11 Answers11

12

It means that the variable is a reference. There is no direct equivalent in C. You can think of it as a pointer that is automatically dereferenced when used, and can never be NULL, maybe.

The typical way to represent a string in C is by a char pointer, so the function would likely look like this:

void read_wav(struct Read_wav* instance, const char *filename)
{
}

Note: the first argument here simulates the implicit this object reference you would have in C++, since this looks like a member method.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    you need a reference to the original struct as well -- based on the usage, internals from the Read_wav class are being used – Foo Bah Feb 09 '11 at 15:21
  • although unlikely, `std::string const& filename` might contain embedded null characters, to perfectly emulate `std::string` you'll need to supply an explicit `length` parameter. Or, like I did, go for the `bstring` library. – Matthieu M. Feb 09 '11 at 15:32
12

In that context, the & makes the variable a reference.

Usually, when you pass an variable to a function, the variable is copied and the function works on the copy. When the function returns, your original variable is unchanged. When you pass a reference, no copy is made and changes made by the function show up even after the function returns.

C doesn't have references, but a C++ reference is functionally the same as a pointer in C. Really the only difference is that pointers have to be dereferenced when you use them:

    *filename = "file.wav";

But references can be used as though they were the original variable:

    filename = "file.wav";

Ostensibly, references are supposed to never be null, although it's not impossible for that to happen.

The equivalent C function would be:

     void read_wav(const char* filename)
     {

     }

This is because C doesn't have string. Usual practice in C is to send a pointer to an array of characters when you need a string. As in C++, if you type a string constant

    read_wav("file.wav");

The type is const char*.

Brian
  • 2,511
  • 20
  • 26
  • "*Ostensibly, references are supposed to never be null, although it's not impossible for that to happen.*" That is UB. If you spot a null reference while debugging, the program is likely broken with memory corruption happening. – Acorn Jan 02 '21 at 04:53
1

In C, you would write it this way:

void read_wav(struct Read_wav* , const char * pSzFileName)
{

}

std::string is the C++ way of dealing with array of const char.

& is a C++ reference. It behaves just as if you were handling the pointer behind ( its is mainly a syntactic sugar, thought it prompts the contract that the pointer behind should not be null).

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • 2
    You'd only write it that way if it was `static`. A non-static member function would require an explicit `Read_wav*` argument to replace the implicit `this` from C++. – dan04 Feb 09 '11 at 14:43
1

The ampersand is used in two different meanings in C++: obtaining an address of something (e.g. of a variable or a function) and specifying a variable or function parameter to be a reference to an entity defined somewhere else. In your example, the latter meaning is in use.

C does not have strictly speaking anything like the reference but pointers (or pointers to pointers) have been user for ages for similar things.

See e.g. http://www.cprogramming.com/tutorial/references.html, What are the differences between a pointer variable and a reference variable in C++? or http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29 for more information about references in C++.

Community
  • 1
  • 1
jrnos
  • 11
  • 1
1

In this particular case, std::string has a c_str method which returns a const char *. You can make a parallel C version and then have your C++ do something like:

void Read_wav::read_wav(const string &filename)
{
    do_read_wav(internal_read_wav, filename.c_str());
}

where do_read_wav is your C routine and internal_read_wav is a pointer to a C-style struct.

void do_read_wav(struct Read_wav rw, const char * filename)

Now, if you are storing information in the class, you need to make a C struct [all of the fields must be POD, etc]

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
1

References are aliases, they are very similar to pointers.

std::string is an array of char with an explicit length (that is, there can be null characters embedded within).

There is a C-library to emulate std::string (that is, provide an encapsulated interface) called bstring for Better String Library. It relieves you from the tedium of having to deal with two distinct variables (array and length).

You cannot use classes in C, but you can emulate them with forwarded struct (to impose encapsulation) and class methods simply become regular functions with an explicit parameter.

Altogether, this leads to the following transformation:

void Read_wav::read_wav(const string &filename);

void read_wav(struct Read_wav* this, struct bstring const* filename);

Which (apart from the struct noise) is very similar to what you had before :)

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
0

& means that variable is passed via reference. So, inside your function you will have not a local copy of variable, but the original variable itself. All the changes to the variable inside the function will influence the original passed variable.

Kos
  • 364
  • 2
  • 3
  • well it's true but in his case as there is a const-qualifier, the variable will not be changed – Nikko Feb 09 '11 at 14:38
0

You might want to check out Binky Pointer fun, it's a video that illustrates what Pointers and References are.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
0

In this case, string &filename means the function will receive a reference (a memory address) as a parameter, instead of receiving it as a copy.

The advantage of this method is that your function will not allocate more space on the stack to save the data contained in filename.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

It's a reference, as others have said. In C, you'd probably write the function as something like

void read_wav(struct Read_wav* rw, const char* filename)
{

}
Chris Card
  • 3,216
  • 20
  • 15
-3

&filename means this is a reference to filename.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kefeizhou
  • 6,234
  • 10
  • 42
  • 55