Home c++ Passing a pointer or reference to a function

Passing a pointer or reference to a function [duplicate]

Author

Date

Category

What’s the difference if you pass a pointer and a link to a function?
for example

# include & lt; iostream & gt;
void func (int * a)
{
 * a = 5
}
int main ()
{
 int a;
 func (a);
 std :: cout & lt; & lt; a;
}

and

# include & lt; iostream & gt;
void func (int & amp; a)
{
 a = 5
}
int main ()
{
 int a;
 func (a);
 std :: cout & lt; & lt; a;
}

Programs will output the same thing?
After all, and so, and so we refer to an existing element, and not create a new one.


Answer 1, authority 100%

Basically, the technical difference between a pointer and a link is that the link itself cannot be changed (but the pointer can). [There is a larger list of differences here .] Besides, the difference is also syntactic: you refer to a link as if it were a variable, and with a pointer, you need to get / dereference it correctly:

void func (int * a)
{
 * a = 5;
}
int main ()
{
 int a;
 func (& amp; a);
 cout & lt; & lt; a;
}

There are no other technical differences and the result will be the same.

But the difference is not really in the syntax, but in the sense, in the semantics.

A pointer can mean anything. In C and C++, it means a string, an assive, a variable address, passing a variable by reference, and a bunch of other things. And the meaning of the link is exactly one – it’s like an alternative name (alias) of an existing variable.

Therefore, for cases like the one you described, it is appropriate in C++ to use a reference rather than a pointer. Although, as you can see for yourself, it works great with a pointer too.

And one more thing: there are no references in pure C, so you have no other option than pointers.


Answer 2, authority 70%

The answers to your question are given above, there is no difference in behavior, but I would like to clarify a few nuances.
1. While you have simple types, there is no difference (except for the one described above in other answers, for example, about the NULL value of a variable in the case of a pointer).
But once it comes to classes, there is a difference.
If a pointer to a class is passed to a function, then, depending on the implementation and the tasks being solved, the pointer will be dereferenced. The complexity of this procedure will depend on the size of the class. This is valid in the case of dereferencing the entire class and does not concern access to its individual members. For example: int c = b- & gt; d; // cheap and A b = * A; // more expensive
2. you can overload the & amp; operator, but * cannot.
3. Pointers cannot take temporary values, while const & amp; can. For example: void f (const & amp; A); .... f (A ()); // maybe
4. In general, links are easier to work with and therefore lead to fewer errors

The golden rule of the C++ programmer: use links where you can, and pointers where you need to.


Answer 3, authority 20%

If you correct typos and include a heading

# include & lt; iostream & gt;
void func (int * a)
{
 * a = 5;
 ^^ ^
}
int main ()
{
 int a;
 func (a);
 std :: cout & lt; & lt; a;
 ^^^^^
}

and

# include & lt; iostream & gt;
void func (int & amp; a)
{
 a = 5;
    ^
}
int main ()
{
 int a;
 func (a);
 std :: cout & lt; & lt; a;
 ^^^^^
}

Then the output will be identical.

When using pointers, you can pass a null pointer. For example,

void func (int * a)
{
  if (a) * a = 5;
}

And the function can be called like

int x;
func (& amp; x);
func (nullptr);

When using links, the link must point to an existing object.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions