In what specific situations is pass by reference usually used? In C

Updated on psychology 2024-05-16
9 answers
  1. Anonymous users2024-02-10

    1.When you need to return a reference object, you need to pass a reference argument to the function.

    2.You need to send a reference parameter to the non-return parameter when the function continues to maintain the median value of the function after the function is executed.

    3.When a function needs to directly process the transfer parameters, the reference parameters are transferred.

  2. Anonymous users2024-02-09

    Functions in c are sometimes passed and returned as per-reference arguments. For example:

    int& f(int& a)

    Passing arguments by reference" is well understood:

    In the usual "pass-by-finger" approach, the operation within a function is actually a copy of the input parameter (i.e., the function creates a temporary variable with the exact same value as the input parameter), and the operation on that copy does not affect the value of the input parameter.

    Passing by reference, on the other hand, can be understood as a copy of a reference variable that shares a storage area with the input parameters. Therefore, a modification of the copy within a function is equivalent to a modification of the input parameters.

    When the input argument is an abstract data type, using "pass by reference" avoids the call of the constructor when creating a copy (and the call of the destructor when the function returns). Therefore, passing by reference is more efficient than passing by value.

    And "return function value by reference" is a bit tricky:

    "Press Finger Return" is also actually creating a temporary variable in the function that is used to return.

    1) If the return value type is an internal type, then the compiler will usually put the return value in the register when the function returns, and then take it out of the register and assign it to the corresponding variable.

    2) If the return value type is an abstract type, register may not have enough space to store the data that returns the value. At this time, the compiler will return the address of the value to the stack after pressing the input parameters to the stack and before calling the function. This writes the data directly to the position of the return value in the stack when the function returns.

    On the other hand, "return by reference" has certain restrictions on the return value: that is, the return value cannot be a local variable, and must be able to survive outside the scope of the function.

    For example, in the following three cases, the first one is false, and the last two are true:

    int& f(int& a)

    int& f(int& a)

    int& f(int& a)

    That is, the store of the return value passed by reference is outside the function, so the operation is much simpler, the compiler keeps operating on the address of the return value during the function call, and when the function is returned at the end, what is returned is actually the value of the return value in the store outside the function.

  3. Anonymous users2024-02-08

    var a = 1;

    var b = a;Copy the value of a.

    b++;alert(a) the value of a is still 1, and it will not change to see ar a = [1];

    var b = a;It is a reference to a b[0]+

    alert(a); //"2"The modification of b also works for a, but of course b = [2]; This modification is useless against a...

    Reference and copy (value and address).

    Passing values is replication.

    The original value will be changed.

    I don't know if the landlord understands?

  4. Anonymous users2024-02-07

    Functionally similar, the value of an object can be passed back to the tonal function.

    But in reality, there is a difference.

    1 System overhead is different.

    When passing references, the system does not have any additional overhead for the parameters passed over, and directly uses the memory space of the original variable.

    When you pass an address, you're essentially passing a pointer variable. In order to store this pointer variable, a pointer variable space is opened when the function is called.

    2 Different forms.

    To pass an object value of type t:

    a.When passing a reference, the function argument needs to be written as t&a; The object itself is passed directly when the function is called; When assigning a value to a function, you can directly assign a value.

    b.When passing an address, the function parameter needs to be written as t*p; When calling a function, you need to pass in the object address; A value of *p needs to be assigned to the assignment.

    From the above comparison, it can be seen that the call of a reference is simpler and more efficient than the call of an address. The reason why the call of the transmission address is reserved is mainly for the compatibility of the C language. When programming in C++, you should try to pass the reference instead of the address.

  5. Anonymous users2024-02-06

    There are similarities, but also differences.

    Therefore, when the value of the parameter is modified inside the function, the modified result can be returned to the caller.

    2) The reference will always point to an object, and the pointer may be null, so the reference is safer.

  6. Anonymous users2024-02-05

    The form parameter is a reference, and when called with a real argument, it is actually passing the address of the actual argument.

  7. Anonymous users2024-02-04

    The principle is the same, but the syntax is different.

  8. Anonymous users2024-02-03

    There are no references in C, only C++. In C++, the reference is to define an alias for a variable, and to manipulate this alias is to manipulate the original variable.

    For example, let's define a reference:

    int a=10;Define a common variable int &ref=a; Define a reference to variable a ref=20; Manipulating the ref here is actually manipulating the a.

    Variables are mainly very useful when passing parameters, when your function parameter is a value parameter, our general practice is to pass a pointer into it, which causes us to operate in the function are all pointers, which is actually very inconvenient, if you use a reference can be very convenient.

    For example, defining a function whose arguments are references.

    void func(int &b)

    Invoke. int a=100;

    func(a);When calling, you can directly pass the parameters into the above method to achieve the same effect as a pointer, but it is more convenient.

  9. Anonymous users2024-02-02

    In C, there is no general idea of reference, this is the concept of C++.

    In the function arguments of C++, there are two forms, where:

    typevar, which is called the referencing method;

    typevar, which is called passing.

    The difference between the two is that when a reference is passed, the form parameter that is actually passed to the function is a reference to the actual parameter, rather than just the value passed to the function. The specific performance is as follows:

    When passing a reference, the parameters and arguments are the same variable, that is, they use the same memory space and have the same address. The addresses of the two are different when transferring the value;

    When passing references, there are no new variables, so there is no construction and destruction of class object parameters. If it is a value transfer call, it will be constructed when it is called, and it will be deconstructed when it exits the function.

    Since the reference uses the address of the original argument, the modification of the reference parameter value will be reflected in the major key function after the exit function is buried, and the modification of the parameter by the transfer value call will not affect the major key function.

Related questions
22 answers2024-05-16

Please note:1 Try to avoid colds.

2 Use a jar cup to smoke your nose with hot water to reduce nasal congestion and discomfort. >>>More

6 answers2024-05-16

Auroras are most commonly found in two annular zones near 67° north-south magnetic latitude, known as the Southern Aurora Zone and the Northern Lights Zone. The northern hemisphere is dominated by Alaska, northern Canada, Siberia, Greenland, the southern tip of Iceland and the north coast of Norway. The Southern Hemisphere, on the other hand, is concentrated near Antarctica. It is worth mentioning that: >>>More

15 answers2024-05-16

The Olympics are divided into winter and summer.

Summer, as the name suggests, is generally held in the summer (summer in the Northern Hemisphere and winter in the Southern Hemisphere) around August. The Winter Olympics are of course held in the winter, usually around February. >>>More

20 answers2024-05-16

I'm pretty much the same to everyone, what's the use of her being beautiful or not? She's ugly, but she fits you very well, wouldn't it be a pity not to socialize when it's suitable to let go and go to dating? >>>More

12 answers2024-05-16

When I chose my other half, I only had three requirements: similar age, not ugly appearance, hardworking and simple. The later wife met the requirements, but now she is an old woman. >>>More