C language and C++ function parameter passing: address results of variables, pointers, arrays, and structures (formal parameters and actual parameters)

insert image description here
Conclusion: Whatever the value passed by the actual parameter, the formal parameter will receive the value (whether it is an address or a number), and the formal parameter is temporarily created in the system stack of the function, and the formal parameter has a new address.
It should be noted that the structure is also a data type. If the actual parameter is a structure and you want to pass the address, you need to add & in front (same as the basic data type)

#include <stdio.h>

void parameter_try(int arr[],int *p,int x)
{
    
    
    printf("\n\n形参数组arr 的首元素地址:%d(相同)",arr);
    printf("\n形参指针p 指向的地址:%d(相同)",p);
    printf("\n形参指针p 的地址:%d(不相同)",p);
    printf("\n形参x 的地址:%d(不相同)",&x);
}

int main()
{
    
    
    int arr[]={
    
    1,2},*p,x=0;
    printf("\n实参数组arr 的首元素地址:%d",arr);
    printf("\n实参指针p 指向的地址:%d",p);
    printf("\n实参指针p 的地址:%d",&p);
    printf("\n实参x 的地址:%d",&x);
    parameter_try(arr,p,x);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_47964723/article/details/123172034
Recommended