primers

Article is the C language function and parameter の structure array “referred to the C language function parameter passing way, baidu has a big circle, has said two, there are three, I’m dizzy, can pass” value and address “there is no doubt that two points, one of the controversial place is the reference method. I am optimistic that write support “pass by reference” way of all the source code posted, and print information, really can not be true ah! Can printing be deceptive? There must be a ghost somewhere! I’m gonna get to the bottom of this! Look who is Li Kui and who is Li GUI!

screening

After many tests, I finally found that the compiler environment (GCC and g++ compiler) is related, here is the comparison verification process:

GCC 9.1.0

#include<stdio.h>  
void myswap(int &x, int &y)  / / reference
{  
    int t;  
    t=x;  
    x=y;  
    y=t;  
}  
  
int main(a) 
{  
    int a = 0, b = 0;  
    myswap(a,b);  // take the arguments a and b directly
    printf("The result of calling the swap function is: %d and %d\n", a, b);  
    return 0;  
} 
Copy the code

Error:

Jdoodle. C: 11: error: expected '; 'and', 'or') 'before' & 'token 2 | void myswap (int & x, int & y) / / the reference | ^ jdoodle. C: In the function' main ': jdoodle. C: which went: Warning: the implicit declaration of the function 'myswap [- Wimplicit - function - declaration] 13 | myswap (a, b); / / directly to exchange variables a and b as arguments to | ^ ~ ~ ~ ~ ~Copy the code

g++14

#include<stdio.h>  
void myswap(int &x, int &y)  / / reference
{  
    int t;  
    t=x;  
    x=y;  
    y=t;  
}  
  
int main(a)  
{  
    int a = 0, b = 1;  
    myswap(a,b);  // take the arguments a and b directly
    printf("The result of calling the swap function is: %d and %d\n", a, b);  
    return 0;  
}  
Copy the code

through

The result of calling the swap function is: 1 and 0Copy the code

conclusion

It turns out that they used the g++ compiler to demonstrate the C language’s support for reference passing, so the compiler passed, resulting in the wrong conclusion. Finally, we reiterate that “pass by reference as a function parameter” is a C++ feature, C language does not support! . In the future we should say: “C language function parameters pass in two ways: pass value, pass pointer; C++ function arguments are passed in three ways: by value, by pointer, by reference.

supplement

Another is that function overloading is only supported by C++ (function overloading is a static polymorphism) and not supported by C. For C, C++ mixed programming needs to pay attention to.

Chapter and verse

GCC differs from GCC, g++

GCC:GNU Compiler Collection(GUN Compiler Collection), it can compile C, C++, JAV, Fortran, Pascal, object-C, Ada, etc. GCC is the GUN C++ Compiler in GCC.