The C shape participates in the variable parameters, and the C shapes participate in the real parame

Updated on technology 2024-02-08
17 answers
  1. Anonymous users2024-02-05

    To change the value of a variable by passing a function, you must pass an address. Your program is passing the value of the variable swap(a,b); If you want to change the value of a variable, you must pass the address of the variable, or do it by a pointer, if it is an array, you can directly use the array name, but if it is an element of the array, you still need to use the address. Just change it like the one upstairs did him.

    The question you asked is really simple, I'm a beginner who hasn't learned for a long time, read more books, this question is very basic. If you don't understand this when you use the pointer in the future, you won't be saved!

  2. Anonymous users2024-02-04

    It is possible to use a pointer to bring the result of the exchange back to the main program.

    void swap

    float t;

    t= *x;

    x = *y;

    y = t; }

    void main

    swap( &a, &b);

  3. Anonymous users2024-02-03

    This is the problem of form parameters and real parameters, swap(a,b) look at this sentence, this sentence calls the above swap function, the value of x,y becomes a,b, which is equivalent to x=a,y=b, the value of x,y is swapped in the function, so y=a, x=b, but the function only changes xyah a, b does not change, so the final output ab is still 70,40

    If you add a cout < to the swap function< x<

  4. Anonymous users2024-02-02

    Because int a, int b is only operated by the parameter address, and the parameter address is different from the original variable address, naturally the original variable is not involved (you can output the function swap a, b to try, they have been exchanged), and the pointer is to operate on the original variable address, that is, directly to the main a and b operations, so it can be exchanged.

  5. Anonymous users2024-02-01

    If the swap function is called, the value of the variable is passed, but the address of the variable is not passed, and the value of the variable cannot be changed. To make a=70 b=40, you can use void swap(float &x, float &y) or void swap(float *x, float *y).There is a detailed explanation in the C++ programming book.

  6. Anonymous users2024-01-31

    void swap only passes the value of the variable, but does not pass the address of the variable, so naturally the value of the variable cannot be changed.

    Upstairs is right, and if you take a closer look at the C books, it's all basic.

  7. Anonymous users2024-01-30

    You take a good look at Tan Haoqiang's C language, the introduction is very clear.

  8. Anonymous users2024-01-29

    I also wondered for a long time when I was learning C, and I didn't understand many people after asking them!

    In the end, after seeing more, I understood it without explanation!

  9. Anonymous users2024-01-28

    Remember: functions are all value transfers. A parameter is a copy of the value of the original variable that is passed. But you have to figure out what value you're passing.

    a, ordinary variable time value passing, well, no problem. You can't change the value of the original variable.

    b, if it is a pointer, it is a copy of the address, which is actually a value pass (value pass of the address). You can't change the value of the original variable (address), but you can (not necessarily) change what the address points to, i.e. the so-called arguments are changed...

    c, if it is a reference, it is also an address that is passed, but this address has been parsed, so it can be used directly, and there is no need to add * to parse (the address value is passed, and it has been parsed when it is used). Changing the value is the same as a pointer, but it's more obvious if you can't change the address (the reference is actually a parsed const pointer).

    1. The value is passed, and the original value cannot be modified. Space is allocated.

    2, you mean char* p=0; char* &p2=p;What does it look like? That's right, you can do this, then you use p2 is the same as using p, the meaning of the expression is to pass as a parameter, you can change the value of the parameter, that is, you can change the address of the parameter pointer... And sometimes the function needs to pass a parameter to change the address, and the parameter can be char* &p2 like this (you can also char**, but you need to parse the address and use it).

    Then you can change the address in the function, and then the original pointer (i.e., the value of the pointer (address)) is changed. Actually, the principle is the same as what I said above. But there is a lot of weight*

  10. Anonymous users2024-01-27

    It is a value passing, and the space is allocated when the function is called, and it is released after the call.

  11. Anonymous users2024-01-26

    For the value transfer call, the value of the argument is passed to the parameter, and the parameter is allocated space, and any operation you do on the parameter will not change the value of the parameter.

    For the address call, it means that the address of the argument is passed, and no new space is allocated for the argument (in fact, I personally think that the argument has a space to store the address of the argument), and any change to the argument here will affect the argument and make the argument change accordingly.

  12. Anonymous users2024-01-25

    First of all, there is a mistake in your ** above! int swap(int &a, int &b) function?

    The function compilation will not pass (if the value is not returned, it is okay to use void, see 2 for modification) void swap(int &a, int &b).Secondly, it is possible to make an exchange! 3.int &a, int &b, is a form parameter, to put it bluntly, it is used for placeholders, when you pass in parameters (real parameters), generally speaking, the real parameters will be copied to the form parameters, but you use a reference, there is no problem of copying, & is equivalent to taking the address, the function exchanges the reference, that is, it is equivalent to the original x and y have also changed accordingly!

    4.Run screenshot:

    If you're new to C++, learn about pointers and references. It's not bad to follow some tech blogs too, it's best to get into the habit of blogging from the beginning, you'll thank me! ☺

  13. Anonymous users2024-01-24

    This is because when the swap function is called, the parameters a and b get a copy of the values of x and y. Numerically, a is equal to x, and b is equal to y, but in fact, different addresses are occupied in memory, so swap(x,y); Here's something it looks like (the compiler doesn't handle it this way, and there are even multiple ways to handle it, just for ease of understanding here):

    int a = x;

    int b = y;

    int t = a;

    a = b;

    b = t;

    So x and y are not swapped. If you want to change the value of a variable outside the function through a function, such as the swap here, you can use a pointer or a reference.

    The reference is relatively simple, which is to change the list of parameters of the function to something like this:

    void swap(int &a, int &b) does not need to change the rest.

    If you use a pointer, you need to get the address of the variable at the call place, and use indirect references in functions, which is more troublesome and less efficient than references.

  14. Anonymous users2024-01-23

    #include

    using namespace std;

    int swap(int &a,int &b)int main(){

    int x,y;

    cin>>x>>y;

    swap(x,y);

    cout<

    No problem, x,y will be exchanged.

    But your understanding is not correct x,y is not a problem with assignment.

    int swap(int &a,int &b) is a function argument that uses a reference.

    In other words, a, b, x, and y are not assignments, but the same thing just has a different name.

    It is equivalent to giving x and y a nickname, which is essentially the same.

  15. Anonymous users2024-01-22

    3.int &a, int &b, is a form parameter, to put it bluntly, it is used for placeholders, when you pass in parameters (real parameters), generally speaking, the real parameters will be copied to the form parameters, but you use a reference, there is no problem of copying, & is equivalent to taking the address, the function exchanges the reference, that is, it is equivalent to the original x and y have also changed accordingly!

  16. Anonymous users2024-01-21

    Here are two methods of passing function parameters:

    1. What you start to learn is value transfer, that is, the parameters copy the values of the arguments, they are basically two different variables, but the values are the same, so any operation on the arguments does not affect the arguments.

    2. The name of the array is the address of the first element of the array, which is the pointer passing, and the address of the array is passed, and the custom function gets the address to operate on the array, which will affect the array. (Note that there is no copy here, but indirect access to the array itself).

  17. Anonymous users2024-01-20

    It's the argument to the function.

    f2{v1,v2) is a return value, that is, a value, and the following is the same.

    The middle ones (v3, v4, v5) are also one. The receiving argument should be a struct or something like that, you can look at the number of commas in parentheses where the func is located.

Related questions