地址运算符:&

本文内容来自《C Prime Plus(第五版)中文版》第233页。


C中最重要的(有时也是最复杂的)概念之一就是指针(pointer),也就是用来存储地址的变量。一元运算符&可以取得变量的存储地址。假设pooh是一个变量的名字,那么&pooh就是该变量的地址。一个变量的地址可以被看做是改变量在内存中的位置。假定使用了以下语句:

pooh=24;

并且假定pooh的存储位置是0B76(PC的地址一般以3位十六进制数的形式表示)。那么语句:

printf("%d%p\n",pooh,&pooh);

将输出如下数值(%p是输出地址的说明符):

240876

看下面例子:

/* loccheck.c  -- checks to see where variables are stored  */
#include <stdio.h>
void mikado(int);                      /* declare function  */
int main(void)
{
    int pooh = 2, bah = 5;             /* local to main()   */

    printf("In main(), pooh = %d and &pooh = %p\n",
            pooh, &pooh);
    printf("In main(), bah = %d and &bah = %p\n",
            bah, &bah);
    mikado(pooh);
    
    return 0;
}

void mikado(int bah)                   /* define function   */
{
    int pooh = 10;                     /* local to mikado() */

    printf("In mikado(), pooh = %d and &pooh = %p\n",
            pooh, &pooh);
    printf("In mikado(), bah = %d and &bah = %p\n",
            bah, &bah);
}

其输出结果如下:


In mian(),pooh=2 and %pooh=0x0012ff48

In mian(),bah=5 and %bah=0x0012ff44

In mian(),pooh=10 and %pooh=0x0012ff34

In mian(),bah=2 and% bah=0x0012ff40


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值