码迷,mamicode.com
首页 > 其他好文 > 详细

int a[5]={}, &a+1与(int*)a+1的区别

时间:2015-09-09 21:06:23      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream> 
#include <typeinfo> 
using namespace std;
  
int  main()  
{ 
    int b, *pb;  
    char *pb2;  
    char *pb3;  
    //&b = 0x001af74
    pb = &b + 1;  //0x001af78   int*
    pb2 = (char*)&b + sizeof(b);  //0x001af78  char*
    pb3 = (char*)&b + 1; //0x001af75   char*
    printf("&b=%#x, pb=%#x, pb2=%#x,pb3=%#x\n", &b, pb, pb2, pb3);  
  
    int a[5]={1,2,3,4,5}, *pa;  
    char *pa2;  
    //&a = 0x002cfc00    int[5]*
    pa = (int*)(&a + 1);  //pa = 0x002cfc14  int*
    pa2 = (char*)&a + sizeof(a);  //pa2 = 0x002cfc14   char*
    //(int*)&a + 1 = 0x002cfc04          &a + 1 = 0x002cfc14
    printf("\nDiff: &a=%#x, %#x, %#x\n", &a, (int*)&a + 1, &a + 1); // 两者是有区别的  
  
    printf("&a=%#x, pa=%#x, pa2=%#x\n", &a, pa, pa2);  
    printf("Equal2=%d\n", (void*)pa == (void*)pa2 );  
  
    puts("结论: &a + 1 == (char*)&a + sizeof(a)\n");  
  
    return 0;
}

 

int a[5]={}, &a+1与(int*)a+1的区别

标签:

原文地址:http://www.cnblogs.com/yaowen/p/4795763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!