zoukankan      html  css  js  c++  java
  • C语言中关于地址传递的理解

    指针: 变量的地址。

    定义: 

     int *p; 这里是一个野指针,会报错。因为它没有指向

     --

    int a=10;

    int *p=&a;  这里就是对的。

    通过 p 来输出一个十六进制的地址。 p将得到 0x-----(-为不确定)

    通过*p 来输出 指针实际对应的值。 *p 将得到10

    #include <stdio.h>
    void fun(
    int *m, int *n);
    int main()
    {
        
    int a = 3, b = 5;
        printf ("a =%d; b = %d \n",a,b);
        fun(&a,&b);
        printf ("a = %d; b =%d \n",a,b);

        
    int k=20;
        int *p = &k;
        printf("%p\n",p); //p is a pointer but the *p is get the value of p
        printf ("%d\n",*p);
        return 0;
    }
    //交换两个数,要操作的是数,而不是指操作指针。传递参数时,采用了地址传递。
    //地址传递要用指针来接收。  获取指针对应的值,用*号来获取
    ;
    void fun(int *m, int *n) 
    {
        
    int t;
        t = *m;
        *m = *n;
        *n = t;
    }

    输出结果为

    a =3; b = 5
    a = 5; b =3
    0xbfbf9db0
    20
  • 相关阅读:
    对象拷贝-深拷贝
    eclipse安装桌面快捷方式
    ajax 分页
    单例模式
    过滤器
    ajax参数详解
    json
    反射
    jdbc02
    jdbc --例子7
  • 原文地址:https://www.cnblogs.com/wenming205/p/2040187.html
Copyright © 2011-2022 走看看